summaryrefslogtreecommitdiff
path: root/llm.go
diff options
context:
space:
mode:
authorGrail Finder <wohilas@gmail.com>2025-11-28 14:35:13 +0300
committerGrail Finder <wohilas@gmail.com>2025-11-28 14:35:13 +0300
commit4bd76fc7d5b69252f5391aab193a59f7ca82a38b (patch)
tree97846b889835924f007ea9e6963b96ab45d9a73a /llm.go
parent7b8505174488d9475ad3594cd355a08e1d2ba5a8 (diff)
parent860160ea0e4d940eee43da8f20538293612093a5 (diff)
Merge branch 'enha/or-tools'HEADmaster
Diffstat (limited to 'llm.go')
-rw-r--r--llm.go36
1 files changed, 24 insertions, 12 deletions
diff --git a/llm.go b/llm.go
index 38b484f..e7245ce 100644
--- a/llm.go
+++ b/llm.go
@@ -76,7 +76,6 @@ type OpenRouterChat struct {
Model string
}
-
func (lcp LlamaCPPeer) GetToken() string {
return ""
}
@@ -161,11 +160,14 @@ func (op OpenAIer) ParseChunk(data []byte) (*models.TextChunk, error) {
Chunk: llmchunk.Choices[len(llmchunk.Choices)-1].Delta.Content,
}
if len(llmchunk.Choices[len(llmchunk.Choices)-1].Delta.ToolCalls) > 0 {
- resp.ToolChunk = llmchunk.Choices[len(llmchunk.Choices)-1].Delta.ToolCalls[0].Function.Arguments
- fname := llmchunk.Choices[len(llmchunk.Choices)-1].Delta.ToolCalls[0].Function.Name
+ toolCall := llmchunk.Choices[len(llmchunk.Choices)-1].Delta.ToolCalls[0]
+ resp.ToolChunk = toolCall.Function.Arguments
+ fname := toolCall.Function.Name
if fname != "" {
resp.FuncName = fname
}
+ // Capture the tool call ID if available
+ resp.ToolID = toolCall.ID
}
if llmchunk.Choices[len(llmchunk.Choices)-1].FinishReason == "stop" {
if resp.Chunk != "" {
@@ -469,6 +471,22 @@ func (or OpenRouterChat) ParseChunk(data []byte) (*models.TextChunk, error) {
resp := &models.TextChunk{
Chunk: llmchunk.Choices[len(llmchunk.Choices)-1].Delta.Content,
}
+
+ // Handle tool calls similar to OpenAIer
+ if len(llmchunk.Choices[len(llmchunk.Choices)-1].Delta.ToolCalls) > 0 {
+ toolCall := llmchunk.Choices[len(llmchunk.Choices)-1].Delta.ToolCalls[0]
+ resp.ToolChunk = toolCall.Function.Arguments
+ fname := toolCall.Function.Name
+ if fname != "" {
+ resp.FuncName = fname
+ }
+ // Capture the tool call ID if available
+ resp.ToolID = toolCall.ID
+ }
+ if resp.ToolChunk != "" {
+ resp.ToolResp = true
+ }
+
if llmchunk.Choices[len(llmchunk.Choices)-1].FinishReason == "stop" {
if resp.Chunk != "" {
logger.Error("text inside of finish llmchunk", "chunk", llmchunk)
@@ -484,16 +502,9 @@ func (or OpenRouterChat) GetToken() string {
func (or OpenRouterChat) FormMsg(msg, role string, resume bool) (io.Reader, error) {
logger.Debug("formmsg open router completion", "link", cfg.CurrentAPI)
-
// Capture the image attachment path at the beginning to avoid race conditions
// with API rotation that might clear the global variable
localImageAttachmentPath := imageAttachmentPath
-
- if cfg.ToolUse && !resume {
- // prompt += "\n" + cfg.ToolRole + ":\n" + toolSysMsg
- // add to chat body
- chatBody.Messages = append(chatBody.Messages, models.RoleMsg{Role: cfg.ToolRole, Content: toolSysMsg})
- }
if msg != "" { // otherwise let the bot continue
var newMsg models.RoleMsg
// Check if we have an image to add to this message
@@ -536,7 +547,6 @@ func (or OpenRouterChat) FormMsg(msg, role string, resume bool) (io.Reader, erro
Model: chatBody.Model,
Stream: chatBody.Stream,
}
-
for i, msg := range chatBody.Messages {
bodyCopy.Messages[i] = msg
// Standardize role if it's a user role
@@ -544,8 +554,10 @@ func (or OpenRouterChat) FormMsg(msg, role string, resume bool) (io.Reader, erro
bodyCopy.Messages[i].Role = "user"
}
}
-
orBody := models.NewOpenRouterChatReq(*bodyCopy, defaultLCPProps)
+ if cfg.ToolUse && !resume && role != cfg.ToolRole {
+ orBody.Tools = baseTools // set tools to use
+ }
data, err := json.Marshal(orBody)
if err != nil {
logger.Error("failed to form a msg", "error", err)