MCPcopy Index your code
hub / github.com/PatchMon/PatchMon / callOpenAI

Function callOpenAI

server-source-code/internal/ai/service.go:317–359  ·  view source on GitHub ↗

OpenAI

(apiKey, model string, messages []Message, opts *callOptions)

Source from the content-addressed store, hash-verified

315
316// OpenAI
317func callOpenAI(apiKey, model string, messages []Message, opts *callOptions) (string, error) {
318 cfg := providers["openai"]
319 if model == "" {
320 model = cfg.defaultModel
321 }
322 body := map[string]interface{}{
323 "model": model,
324 "messages": messages,
325 "max_tokens": opts.maxTokens,
326 "temperature": opts.temperature,
327 }
328 reqBody, _ := json.Marshal(body)
329 req, err := http.NewRequest("POST", cfg.baseURL+"/chat/completions", bytes.NewReader(reqBody))
330 if err != nil {
331 return "", err
332 }
333 req.Header.Set("Content-Type", "application/json")
334 req.Header.Set("Authorization", "Bearer "+apiKey)
335
336 resp, err := http.DefaultClient.Do(req)
337 if err != nil {
338 return "", err
339 }
340 defer func() { _ = resp.Body.Close() }()
341 data, _ := io.ReadAll(resp.Body)
342 if resp.StatusCode != http.StatusOK {
343 return "", fmt.Errorf("OpenAI API error: %d - %s", resp.StatusCode, string(data))
344 }
345 var out struct {
346 Choices []struct {
347 Message struct {
348 Content string `json:"content"`
349 } `json:"message"`
350 } `json:"choices"`
351 }
352 if err := json.Unmarshal(data, &out); err != nil {
353 return "", err
354 }
355 if len(out.Choices) > 0 {
356 return out.Choices[0].Message.Content, nil
357 }
358 return "", nil
359}
360
361// Gemini
362func callGemini(apiKey, model string, messages []Message, opts *callOptions) (string, error) {

Callers 1

callAIMethod · 0.85

Calls 3

UnmarshalMethod · 0.80
SetMethod · 0.45
CloseMethod · 0.45

Tested by

no test coverage detected