(ctx context.Context, apiKey, model, message string, tools bool)
| 20 | ) |
| 21 | |
| 22 | func makeOpenAIRequest(ctx context.Context, apiKey, model, message string, tools bool) error { |
| 23 | reqBody := openai.OpenAIRequest{ |
| 24 | Model: model, |
| 25 | Input: []any{ |
| 26 | openai.OpenAIMessage{ |
| 27 | Role: "user", |
| 28 | Content: []openai.OpenAIMessageContent{ |
| 29 | { |
| 30 | Type: "input_text", |
| 31 | Text: message, |
| 32 | }, |
| 33 | }, |
| 34 | }, |
| 35 | }, |
| 36 | Stream: true, |
| 37 | StreamOptions: &openai.StreamOptionsType{IncludeObfuscation: false}, |
| 38 | Reasoning: &openai.ReasoningType{Effort: "medium"}, |
| 39 | } |
| 40 | if tools { |
| 41 | reqBody.Tools = []openai.OpenAIRequestTool{ |
| 42 | openai.ConvertToolDefinitionToOpenAI(aiusechat.GetAdderToolDefinition()), |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | jsonData, err := json.Marshal(reqBody) |
| 47 | if err != nil { |
| 48 | return fmt.Errorf("error marshaling request: %v", err) |
| 49 | } |
| 50 | |
| 51 | // Pretty print the request JSON for debugging |
| 52 | prettyJSON, err := json.MarshalIndent(reqBody, "", " ") |
| 53 | if err == nil { |
| 54 | fmt.Printf("Request JSON:\n%s\n", string(prettyJSON)) |
| 55 | } |
| 56 | |
| 57 | req, err := http.NewRequestWithContext(ctx, "POST", "https://api.openai.com/v1/responses", bytes.NewBuffer(jsonData)) |
| 58 | if err != nil { |
| 59 | return fmt.Errorf("error creating request: %v", err) |
| 60 | } |
| 61 | |
| 62 | req.Header.Set("Content-Type", "application/json") |
| 63 | req.Header.Set("Authorization", "Bearer "+apiKey) |
| 64 | req.Header.Set("Accept", "text/event-stream") |
| 65 | |
| 66 | client := &http.Client{ |
| 67 | Timeout: 60 * time.Second, |
| 68 | } |
| 69 | |
| 70 | resp, err := client.Do(req) |
| 71 | if err != nil { |
| 72 | return fmt.Errorf("error making request: %v", err) |
| 73 | } |
| 74 | defer resp.Body.Close() |
| 75 | |
| 76 | fmt.Printf("Response Status: %s\n", resp.Status) |
| 77 | fmt.Printf("Response Headers:\n") |
| 78 | for name, values := range resp.Header { |
| 79 | for _, value := range values { |
no test coverage detected