Skip to content

Commit 7381d18

Browse files
authored
Fix for broken Azure Assistants url (#665)
* fix:fix url for Azure assistants api * test:add unit tests for Azure Assistants api * fix:minor liniting issue
1 parent e8b3478 commit 7381d18

File tree

2 files changed

+191
-1
lines changed

2 files changed

+191
-1
lines changed

assistant_test.go

Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,3 +202,193 @@ When asked a question, write and run Python code to answer the question.`
202202
err = client.DeleteAssistantFile(ctx, assistantID, assistantFileID)
203203
checks.NoError(t, err, "DeleteAssistantFile error")
204204
}
205+
206+
func TestAzureAssistant(t *testing.T) {
207+
assistantID := "asst_abc123"
208+
assistantName := "Ambrogio"
209+
assistantDescription := "Ambrogio is a friendly assistant."
210+
assistantInstructions := `You are a personal math tutor.
211+
When asked a question, write and run Python code to answer the question.`
212+
assistantFileID := "file-wB6RM6wHdA49HfS2DJ9fEyrH"
213+
limit := 20
214+
order := "desc"
215+
after := "asst_abc122"
216+
before := "asst_abc124"
217+
218+
client, server, teardown := setupAzureTestServer()
219+
defer teardown()
220+
221+
server.RegisterHandler(
222+
"/openai/assistants/"+assistantID+"/files/"+assistantFileID,
223+
func(w http.ResponseWriter, r *http.Request) {
224+
if r.Method == http.MethodGet {
225+
resBytes, _ := json.Marshal(openai.AssistantFile{
226+
ID: assistantFileID,
227+
Object: "assistant.file",
228+
CreatedAt: 1234567890,
229+
AssistantID: assistantID,
230+
})
231+
fmt.Fprintln(w, string(resBytes))
232+
} else if r.Method == http.MethodDelete {
233+
fmt.Fprintln(w, `{
234+
id: "file-wB6RM6wHdA49HfS2DJ9fEyrH",
235+
object: "assistant.file.deleted",
236+
deleted: true
237+
}`)
238+
}
239+
},
240+
)
241+
242+
server.RegisterHandler(
243+
"/openai/assistants/"+assistantID+"/files",
244+
func(w http.ResponseWriter, r *http.Request) {
245+
if r.Method == http.MethodGet {
246+
resBytes, _ := json.Marshal(openai.AssistantFilesList{
247+
AssistantFiles: []openai.AssistantFile{
248+
{
249+
ID: assistantFileID,
250+
Object: "assistant.file",
251+
CreatedAt: 1234567890,
252+
AssistantID: assistantID,
253+
},
254+
},
255+
})
256+
fmt.Fprintln(w, string(resBytes))
257+
} else if r.Method == http.MethodPost {
258+
var request openai.AssistantFileRequest
259+
err := json.NewDecoder(r.Body).Decode(&request)
260+
checks.NoError(t, err, "Decode error")
261+
262+
resBytes, _ := json.Marshal(openai.AssistantFile{
263+
ID: request.FileID,
264+
Object: "assistant.file",
265+
CreatedAt: 1234567890,
266+
AssistantID: assistantID,
267+
})
268+
fmt.Fprintln(w, string(resBytes))
269+
}
270+
},
271+
)
272+
273+
server.RegisterHandler(
274+
"/openai/assistants/"+assistantID,
275+
func(w http.ResponseWriter, r *http.Request) {
276+
switch r.Method {
277+
case http.MethodGet:
278+
resBytes, _ := json.Marshal(openai.Assistant{
279+
ID: assistantID,
280+
Object: "assistant",
281+
CreatedAt: 1234567890,
282+
Name: &assistantName,
283+
Model: openai.GPT4TurboPreview,
284+
Description: &assistantDescription,
285+
Instructions: &assistantInstructions,
286+
})
287+
fmt.Fprintln(w, string(resBytes))
288+
case http.MethodPost:
289+
var request openai.AssistantRequest
290+
err := json.NewDecoder(r.Body).Decode(&request)
291+
checks.NoError(t, err, "Decode error")
292+
293+
resBytes, _ := json.Marshal(openai.Assistant{
294+
ID: assistantID,
295+
Object: "assistant",
296+
CreatedAt: 1234567890,
297+
Name: request.Name,
298+
Model: request.Model,
299+
Description: request.Description,
300+
Instructions: request.Instructions,
301+
Tools: request.Tools,
302+
})
303+
fmt.Fprintln(w, string(resBytes))
304+
case http.MethodDelete:
305+
fmt.Fprintln(w, `{
306+
"id": "asst_abc123",
307+
"object": "assistant.deleted",
308+
"deleted": true
309+
}`)
310+
}
311+
},
312+
)
313+
314+
server.RegisterHandler(
315+
"/openai/assistants",
316+
func(w http.ResponseWriter, r *http.Request) {
317+
if r.Method == http.MethodPost {
318+
var request openai.AssistantRequest
319+
err := json.NewDecoder(r.Body).Decode(&request)
320+
checks.NoError(t, err, "Decode error")
321+
322+
resBytes, _ := json.Marshal(openai.Assistant{
323+
ID: assistantID,
324+
Object: "assistant",
325+
CreatedAt: 1234567890,
326+
Name: request.Name,
327+
Model: request.Model,
328+
Description: request.Description,
329+
Instructions: request.Instructions,
330+
Tools: request.Tools,
331+
})
332+
fmt.Fprintln(w, string(resBytes))
333+
} else if r.Method == http.MethodGet {
334+
resBytes, _ := json.Marshal(openai.AssistantsList{
335+
LastID: &assistantID,
336+
FirstID: &assistantID,
337+
Assistants: []openai.Assistant{
338+
{
339+
ID: assistantID,
340+
Object: "assistant",
341+
CreatedAt: 1234567890,
342+
Name: &assistantName,
343+
Model: openai.GPT4TurboPreview,
344+
Description: &assistantDescription,
345+
Instructions: &assistantInstructions,
346+
},
347+
},
348+
})
349+
fmt.Fprintln(w, string(resBytes))
350+
}
351+
},
352+
)
353+
354+
ctx := context.Background()
355+
356+
_, err := client.CreateAssistant(ctx, openai.AssistantRequest{
357+
Name: &assistantName,
358+
Description: &assistantDescription,
359+
Model: openai.GPT4TurboPreview,
360+
Instructions: &assistantInstructions,
361+
})
362+
checks.NoError(t, err, "CreateAssistant error")
363+
364+
_, err = client.RetrieveAssistant(ctx, assistantID)
365+
checks.NoError(t, err, "RetrieveAssistant error")
366+
367+
_, err = client.ModifyAssistant(ctx, assistantID, openai.AssistantRequest{
368+
Name: &assistantName,
369+
Description: &assistantDescription,
370+
Model: openai.GPT4TurboPreview,
371+
Instructions: &assistantInstructions,
372+
})
373+
checks.NoError(t, err, "ModifyAssistant error")
374+
375+
_, err = client.DeleteAssistant(ctx, assistantID)
376+
checks.NoError(t, err, "DeleteAssistant error")
377+
378+
_, err = client.ListAssistants(ctx, &limit, &order, &after, &before)
379+
checks.NoError(t, err, "ListAssistants error")
380+
381+
_, err = client.CreateAssistantFile(ctx, assistantID, openai.AssistantFileRequest{
382+
FileID: assistantFileID,
383+
})
384+
checks.NoError(t, err, "CreateAssistantFile error")
385+
386+
_, err = client.ListAssistantFiles(ctx, assistantID, &limit, &order, &after, &before)
387+
checks.NoError(t, err, "ListAssistantFiles error")
388+
389+
_, err = client.RetrieveAssistantFile(ctx, assistantID, assistantFileID)
390+
checks.NoError(t, err, "RetrieveAssistantFile error")
391+
392+
err = client.DeleteAssistantFile(ctx, assistantID, assistantFileID)
393+
checks.NoError(t, err, "DeleteAssistantFile error")
394+
}

client.go

Lines changed: 1 addition & 1 deletion
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") {
224+
if strings.Contains(suffix, "/models") || strings.Contains(suffix, "/assistants") {
225225
return fmt.Sprintf("%s/%s%s?api-version=%s", baseURL, azureAPIPrefix, suffix, c.config.APIVersion)
226226
}
227227
azureDeploymentName := "UNKNOWN"

0 commit comments

Comments
 (0)