MCPcopy Create free account
hub / github.com/docker/model-test / ProcessChatMessage

Method ProcessChatMessage

services/openai_service.go:61–178  ·  view source on GitHub ↗

ProcessChatMessage processes a chat message with test case context for logging

(ctx context.Context, userMessage string, session *models.ChatSession, testCase string)

Source from the content-addressed store, hash-verified

59
60// ProcessChatMessage processes a chat message with test case context for logging
61func (ai *OpenAIService) ProcessChatMessage(ctx context.Context, userMessage string, session *models.ChatSession, testCase string) (*models.ChatResponse, error) {
62 // Generate session ID if not provided
63 sessionID := session.SessionID
64 if sessionID == "" {
65 sessionID = ai.generateSessionID()
66 }
67
68 // Define the tools available to the AI
69 t := ai.getToolDefinitions()
70
71 // Build messages including conversation history
72 messages := ai.buildMessagesFromSession(session, userMessage)
73
74 var cartSummary *models.CartSummary
75 var toolResults []models.ToolCallResult
76 var responseMessage string
77
78 // Track LLM request metrics
79 var llmRequests int
80 var totalLLMTime time.Duration
81
82 // Maximum number of tool call iterations
83 maxIterations := 5
84 currentIteration := 0
85
86 for currentIteration < maxIterations {
87 // Track LLM request time
88 llmStart := time.Now()
89
90 // Prepare request parameters
91 requestParams := openai.ChatCompletionNewParams{
92 Model: ai.defaultModel,
93 Messages: messages,
94 Tools: t,
95 Temperature: param.Opt[float64]{Value: 0},
96 }
97
98 // Create the chat completion request
99 completion, err := ai.client.Chat.Completions.New(ctx, requestParams)
100
101 // Record LLM request metrics
102 llmDuration := time.Since(llmStart)
103 llmRequests++
104 totalLLMTime += llmDuration
105
106 // Log the request/response or error
107 if ai.logger != nil {
108 if err != nil {
109 if logErr := ai.logger.LogError(testCase, currentIteration+1, requestParams, err, ai.baseURL); logErr != nil {
110 fmt.Printf("Failed to log error: %v\n", logErr)
111 }
112 } else {
113 if logErr := ai.logger.LogRequest(testCase, currentIteration+1, requestParams, completion, ai.baseURL); logErr != nil {
114 fmt.Printf("Failed to log request: %v\n", logErr)
115 }
116 }
117 }
118

Callers 1

runAgentTestMethod · 0.80

Calls 7

generateSessionIDMethod · 0.95
getToolDefinitionsMethod · 0.95
LogErrorMethod · 0.80
LogRequestMethod · 0.80
ExecuteToolCallsMethod · 0.80
GetCartSummaryMethod · 0.80

Tested by

no test coverage detected