@@ -18,6 +18,7 @@ import (
1818 "github.com/yomorun/yomo/core/metadata"
1919 "github.com/yomorun/yomo/core/ylog"
2020 "github.com/yomorun/yomo/pkg/bridge/ai/register"
21+ "github.com/yomorun/yomo/pkg/id"
2122 "github.com/yomorun/yomo/serverless"
2223)
2324
@@ -164,7 +165,7 @@ func (s *Service) createReducer() (yomo.StreamFunction, error) {
164165 c , ok := s .sfnCallCache [reqID ]
165166 s .muCallCache .Unlock ()
166167 if ! ok {
167- ylog .Error ("[sfn-reducer] req_id not found" , "req_id" , reqID )
168+ ylog .Error ("[sfn-reducer] req_id not found" , "trans_id" , invoke . TransID , " req_id" , reqID )
168169 return
169170 }
170171
@@ -204,7 +205,7 @@ func (s *Service) GetOverview() (*ai.OverviewResponse, error) {
204205}
205206
206207// GetInvoke returns the invoke response
207- func (s * Service ) GetInvoke (ctx context.Context , userInstruction string , baseSystemMessage string , reqID string , includeCallStack bool ) (* ai.InvokeResponse , error ) {
208+ func (s * Service ) GetInvoke (ctx context.Context , userInstruction string , baseSystemMessage string , transID string , includeCallStack bool ) (* ai.InvokeResponse , error ) {
208209 // read tools attached to the metadata
209210 tcs , err := register .ListToolCalls (s .Metadata )
210211 if err != nil {
@@ -243,8 +244,8 @@ func (s *Service) GetInvoke(ctx context.Context, userInstruction string, baseSys
243244 "res_toolcalls" , fmt .Sprintf ("%+v" , res .ToolCalls ),
244245 "res_assistant_msgs" , fmt .Sprintf ("%+v" , res .AssistantMessage ))
245246
246- ylog .Debug (">> run function calls" , "reqID " , reqID , "res.ToolCalls" , fmt .Sprintf ("%+v" , res .ToolCalls ))
247- llmCalls , err := s .runFunctionCalls (res .ToolCalls , reqID )
247+ ylog .Debug (">> run function calls" , "transID " , transID , "res.ToolCalls" , fmt .Sprintf ("%+v" , res .ToolCalls ))
248+ llmCalls , err := s .runFunctionCalls (res .ToolCalls , transID , id . New ( 16 ) )
248249 if err != nil {
249250 return nil , err
250251 }
@@ -322,7 +323,7 @@ func overWriteSystemPrompt(req openai.ChatCompletionRequest, sysPrompt string) o
322323}
323324
324325// GetChatCompletions returns the llm api response
325- func (s * Service ) GetChatCompletions (ctx context.Context , req openai.ChatCompletionRequest , reqID string , w http.ResponseWriter , includeCallStack bool ) error {
326+ func (s * Service ) GetChatCompletions (ctx context.Context , req openai.ChatCompletionRequest , transID string , w http.ResponseWriter , includeCallStack bool ) error {
326327 // 1. find all hosting tool sfn
327328 tagTools , err := register .ListToolCalls (s .Metadata )
328329 if err != nil {
@@ -386,7 +387,7 @@ func (s *Service) GetChatCompletions(ctx context.Context, req openai.ChatComplet
386387 toolCallsMap [index ] = item
387388 }
388389 isFunctionCall = true
389- } else {
390+ } else if streamRes . Choices [ 0 ]. FinishReason != openai . FinishReasonToolCalls {
390391 _ , _ = io .WriteString (w , "data: " )
391392 _ = json .NewEncoder (w ).Encode (streamRes )
392393 _ , _ = io .WriteString (w , "\n " )
@@ -436,7 +437,8 @@ func (s *Service) GetChatCompletions(ctx context.Context, req openai.ChatComplet
436437 }
437438 }
438439 // 6. run llm function calls
439- llmCalls , err := s .runFunctionCalls (fnCalls , reqID )
440+ reqID := id .New (16 )
441+ llmCalls , err := s .runFunctionCalls (fnCalls , transID , reqID )
440442 if err != nil {
441443 return err
442444 }
@@ -471,9 +473,6 @@ func (s *Service) GetChatCompletions(ctx context.Context, req openai.ChatComplet
471473 if err != nil {
472474 return err
473475 }
474- if len (streamRes .Choices ) == 0 {
475- continue
476- }
477476 _ , _ = io .WriteString (w , "data: " )
478477 _ = json .NewEncoder (w ).Encode (streamRes )
479478 _ , _ = io .WriteString (w , "\n " )
@@ -491,22 +490,23 @@ func (s *Service) GetChatCompletions(ctx context.Context, req openai.ChatComplet
491490}
492491
493492// run llm-sfn function calls
494- func (s * Service ) runFunctionCalls (fns map [uint32 ][]* openai.ToolCall , reqID string ) ([]ai.ToolMessage , error ) {
493+ func (s * Service ) runFunctionCalls (fns map [uint32 ][]* openai.ToolCall , transID , reqID string ) ([]ai.ToolMessage , error ) {
495494 if len (fns ) == 0 {
496495 return nil , nil
497496 }
498497
499498 asyncCall := & sfnAsyncCall {
500499 val : make (map [string ]ai.ToolMessage ),
501500 }
501+
502502 s .muCallCache .Lock ()
503503 s .sfnCallCache [reqID ] = asyncCall
504504 s .muCallCache .Unlock ()
505505
506506 for tag , tcs := range fns {
507- ylog .Debug ("+++invoke toolCalls" , "tag" , tag , "len(toolCalls)" , len (tcs ), "reqID" , reqID )
507+ ylog .Debug ("+++invoke toolCalls" , "tag" , tag , "len(toolCalls)" , len (tcs ), "transID" , transID , " reqID" , reqID )
508508 for _ , fn := range tcs {
509- err := s .fireLlmSfn (tag , fn , reqID )
509+ err := s .fireLlmSfn (tag , fn , transID , reqID )
510510 if err != nil {
511511 ylog .Error ("send data to zipper" , "err" , err .Error ())
512512 continue
@@ -533,19 +533,22 @@ func (s *Service) runFunctionCalls(fns map[uint32][]*openai.ToolCall, reqID stri
533533}
534534
535535// fireLlmSfn fires the llm-sfn function call by s.source.Write()
536- func (s * Service ) fireLlmSfn (tag uint32 , fn * openai.ToolCall , reqID string ) error {
536+ func (s * Service ) fireLlmSfn (tag uint32 , fn * openai.ToolCall , transID , reqID string ) error {
537537 ylog .Info (
538538 "+invoke func" ,
539539 "tag" , tag ,
540+ "transID" , transID ,
541+ "reqID" , reqID ,
540542 "toolCallID" , fn .ID ,
541543 "function" , fn .Function .Name ,
542544 "arguments" , fn .Function .Arguments ,
543- "reqID" , reqID )
545+ )
544546 data := & ai.FunctionCall {
547+ TransID : transID ,
545548 ReqID : reqID ,
546549 ToolCallID : fn .ID ,
547- Arguments : fn .Function .Arguments ,
548550 FunctionName : fn .Function .Name ,
551+ Arguments : fn .Function .Arguments ,
549552 }
550553 buf , err := data .Bytes ()
551554 if err != nil {
0 commit comments