Skip to content

Commit c5401e9

Browse files
authored
Fix for broken Azure Threads url (sashabaranov#668)
1 parent 7381d18 commit c5401e9

File tree

2 files changed

+93
-1
lines changed

2 files changed

+93
-1
lines changed

client.go

+10-1
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ func (c *Client) fullURL(suffix string, args ...any) string {
221221
baseURL = strings.TrimRight(baseURL, "/")
222222
// if suffix is /models change to {endpoint}/openai/models?api-version=2022-12-01
223223
// https://learn.microsoft.com/en-us/rest/api/cognitiveservices/azureopenaistable/models/list?tabs=HTTP
224-
if strings.Contains(suffix, "/models") || strings.Contains(suffix, "/assistants") {
224+
if containsSubstr([]string{"/models", "/assistants", "/threads", "/files"}, suffix) {
225225
return fmt.Sprintf("%s/%s%s?api-version=%s", baseURL, azureAPIPrefix, suffix, c.config.APIVersion)
226226
}
227227
azureDeploymentName := "UNKNOWN"
@@ -258,3 +258,12 @@ func (c *Client) handleErrorResp(resp *http.Response) error {
258258
errRes.Error.HTTPStatusCode = resp.StatusCode
259259
return errRes.Error
260260
}
261+
262+
func containsSubstr(s []string, e string) bool {
263+
for _, v := range s {
264+
if strings.Contains(e, v) {
265+
return true
266+
}
267+
}
268+
return false
269+
}

thread_test.go

+83
Original file line numberDiff line numberDiff line change
@@ -93,3 +93,86 @@ func TestThread(t *testing.T) {
9393
_, err = client.DeleteThread(ctx, threadID)
9494
checks.NoError(t, err, "DeleteThread error")
9595
}
96+
97+
// TestAzureThread Tests the thread endpoint of the API using the Azure mocked server.
98+
func TestAzureThread(t *testing.T) {
99+
threadID := "thread_abc123"
100+
client, server, teardown := setupAzureTestServer()
101+
defer teardown()
102+
103+
server.RegisterHandler(
104+
"/openai/threads/"+threadID,
105+
func(w http.ResponseWriter, r *http.Request) {
106+
switch r.Method {
107+
case http.MethodGet:
108+
resBytes, _ := json.Marshal(openai.Thread{
109+
ID: threadID,
110+
Object: "thread",
111+
CreatedAt: 1234567890,
112+
})
113+
fmt.Fprintln(w, string(resBytes))
114+
case http.MethodPost:
115+
var request openai.ThreadRequest
116+
err := json.NewDecoder(r.Body).Decode(&request)
117+
checks.NoError(t, err, "Decode error")
118+
119+
resBytes, _ := json.Marshal(openai.Thread{
120+
ID: threadID,
121+
Object: "thread",
122+
CreatedAt: 1234567890,
123+
})
124+
fmt.Fprintln(w, string(resBytes))
125+
case http.MethodDelete:
126+
fmt.Fprintln(w, `{
127+
"id": "thread_abc123",
128+
"object": "thread.deleted",
129+
"deleted": true
130+
}`)
131+
}
132+
},
133+
)
134+
135+
server.RegisterHandler(
136+
"/openai/threads",
137+
func(w http.ResponseWriter, r *http.Request) {
138+
if r.Method == http.MethodPost {
139+
var request openai.ModifyThreadRequest
140+
err := json.NewDecoder(r.Body).Decode(&request)
141+
checks.NoError(t, err, "Decode error")
142+
143+
resBytes, _ := json.Marshal(openai.Thread{
144+
ID: threadID,
145+
Object: "thread",
146+
CreatedAt: 1234567890,
147+
Metadata: request.Metadata,
148+
})
149+
fmt.Fprintln(w, string(resBytes))
150+
}
151+
},
152+
)
153+
154+
ctx := context.Background()
155+
156+
_, err := client.CreateThread(ctx, openai.ThreadRequest{
157+
Messages: []openai.ThreadMessage{
158+
{
159+
Role: openai.ThreadMessageRoleUser,
160+
Content: "Hello, World!",
161+
},
162+
},
163+
})
164+
checks.NoError(t, err, "CreateThread error")
165+
166+
_, err = client.RetrieveThread(ctx, threadID)
167+
checks.NoError(t, err, "RetrieveThread error")
168+
169+
_, err = client.ModifyThread(ctx, threadID, openai.ModifyThreadRequest{
170+
Metadata: map[string]interface{}{
171+
"key": "value",
172+
},
173+
})
174+
checks.NoError(t, err, "ModifyThread error")
175+
176+
_, err = client.DeleteThread(ctx, threadID)
177+
checks.NoError(t, err, "DeleteThread error")
178+
}

0 commit comments

Comments
 (0)