|
1 | 1 | package openai
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "strings" |
4 | 5 | "testing"
|
5 | 6 |
|
6 | 7 | "github.com/danielmiessler/fabric/chat"
|
7 | 8 | "github.com/danielmiessler/fabric/common"
|
8 | 9 | openai "github.com/openai/openai-go"
|
| 10 | + "github.com/openai/openai-go/responses" |
9 | 11 | "github.com/openai/openai-go/shared"
|
10 | 12 | "github.com/stretchr/testify/assert"
|
11 | 13 | )
|
@@ -60,3 +62,116 @@ func TestBuildResponseRequestNoMaxTokens(t *testing.T) {
|
60 | 62 | assert.Equal(t, openai.Float(opts.TopP), request.TopP)
|
61 | 63 | assert.False(t, request.MaxOutputTokens.Valid())
|
62 | 64 | }
|
| 65 | + |
| 66 | +func TestBuildResponseParams_WithoutSearch(t *testing.T) { |
| 67 | + client := NewClient() |
| 68 | + opts := &common.ChatOptions{ |
| 69 | + Model: "gpt-4o", |
| 70 | + Temperature: 0.7, |
| 71 | + Search: false, |
| 72 | + } |
| 73 | + |
| 74 | + msgs := []*chat.ChatCompletionMessage{ |
| 75 | + {Role: "user", Content: "Hello"}, |
| 76 | + } |
| 77 | + |
| 78 | + params := client.buildResponseParams(msgs, opts) |
| 79 | + |
| 80 | + assert.Nil(t, params.Tools, "Expected no tools when search is disabled") |
| 81 | + assert.Equal(t, shared.ResponsesModel(opts.Model), params.Model) |
| 82 | + assert.Equal(t, openai.Float(opts.Temperature), params.Temperature) |
| 83 | +} |
| 84 | + |
| 85 | +func TestBuildResponseParams_WithSearch(t *testing.T) { |
| 86 | + client := NewClient() |
| 87 | + opts := &common.ChatOptions{ |
| 88 | + Model: "gpt-4o", |
| 89 | + Temperature: 0.7, |
| 90 | + Search: true, |
| 91 | + } |
| 92 | + |
| 93 | + msgs := []*chat.ChatCompletionMessage{ |
| 94 | + {Role: "user", Content: "What's the weather today?"}, |
| 95 | + } |
| 96 | + |
| 97 | + params := client.buildResponseParams(msgs, opts) |
| 98 | + |
| 99 | + assert.NotNil(t, params.Tools, "Expected tools when search is enabled") |
| 100 | + assert.Len(t, params.Tools, 1, "Expected exactly one tool") |
| 101 | + |
| 102 | + tool := params.Tools[0] |
| 103 | + assert.NotNil(t, tool.OfWebSearchPreview, "Expected web search tool") |
| 104 | + assert.Equal(t, responses.WebSearchToolType("web_search_preview"), tool.OfWebSearchPreview.Type) |
| 105 | +} |
| 106 | + |
| 107 | +func TestBuildResponseParams_WithSearchAndLocation(t *testing.T) { |
| 108 | + client := NewClient() |
| 109 | + opts := &common.ChatOptions{ |
| 110 | + Model: "gpt-4o", |
| 111 | + Temperature: 0.7, |
| 112 | + Search: true, |
| 113 | + SearchLocation: "America/Los_Angeles", |
| 114 | + } |
| 115 | + |
| 116 | + msgs := []*chat.ChatCompletionMessage{ |
| 117 | + {Role: "user", Content: "What's the weather in San Francisco?"}, |
| 118 | + } |
| 119 | + |
| 120 | + params := client.buildResponseParams(msgs, opts) |
| 121 | + |
| 122 | + assert.NotNil(t, params.Tools, "Expected tools when search is enabled") |
| 123 | + tool := params.Tools[0] |
| 124 | + assert.NotNil(t, tool.OfWebSearchPreview, "Expected web search tool") |
| 125 | + |
| 126 | + userLocation := tool.OfWebSearchPreview.UserLocation |
| 127 | + assert.Equal(t, "approximate", string(userLocation.Type)) |
| 128 | + assert.True(t, userLocation.Timezone.Valid(), "Expected timezone to be set") |
| 129 | + assert.Equal(t, opts.SearchLocation, userLocation.Timezone.Value) |
| 130 | +} |
| 131 | + |
| 132 | +func TestCitationFormatting(t *testing.T) { |
| 133 | + // Test the citation formatting logic by simulating the citation extraction |
| 134 | + var textParts []string |
| 135 | + var citations []string |
| 136 | + citationMap := make(map[string]bool) |
| 137 | + |
| 138 | + // Simulate text content |
| 139 | + textParts = append(textParts, "Based on recent research, artificial intelligence is advancing rapidly.") |
| 140 | + |
| 141 | + // Simulate citations (as they would be extracted from OpenAI response) |
| 142 | + mockCitations := []struct { |
| 143 | + URL string |
| 144 | + Title string |
| 145 | + }{ |
| 146 | + {"https://example.com/ai-research", "AI Research Advances 2025"}, |
| 147 | + {"https://another-source.com/tech-news", "Technology News Today"}, |
| 148 | + {"https://example.com/ai-research", "AI Research Advances 2025"}, // Duplicate to test deduplication |
| 149 | + } |
| 150 | + |
| 151 | + for _, citation := range mockCitations { |
| 152 | + citationKey := citation.URL + "|" + citation.Title |
| 153 | + if !citationMap[citationKey] { |
| 154 | + citationMap[citationKey] = true |
| 155 | + citationText := "- [" + citation.Title + "](" + citation.URL + ")" |
| 156 | + citations = append(citations, citationText) |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + result := strings.Join(textParts, "") |
| 161 | + if len(citations) > 0 { |
| 162 | + result += "\n\n## Sources\n\n" + strings.Join(citations, "\n") |
| 163 | + } |
| 164 | + |
| 165 | + // Verify the result contains the expected text |
| 166 | + expectedText := "Based on recent research, artificial intelligence is advancing rapidly." |
| 167 | + assert.Contains(t, result, expectedText, "Expected result to contain original text") |
| 168 | + |
| 169 | + // Verify citations are included |
| 170 | + assert.Contains(t, result, "## Sources", "Expected result to contain Sources section") |
| 171 | + assert.Contains(t, result, "[AI Research Advances 2025](https://example.com/ai-research)", "Expected result to contain first citation") |
| 172 | + assert.Contains(t, result, "[Technology News Today](https://another-source.com/tech-news)", "Expected result to contain second citation") |
| 173 | + |
| 174 | + // Verify deduplication - should only have 2 unique citations, not 3 |
| 175 | + citationCount := strings.Count(result, "- [") |
| 176 | + assert.Equal(t, 2, citationCount, "Expected 2 unique citations") |
| 177 | +} |
0 commit comments