|
| 1 | +//go:build go1.18 |
| 2 | +// +build go1.18 |
| 3 | + |
| 4 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 5 | +// Licensed under the MIT License. See License.txt in the project root for license information. |
| 6 | + |
| 7 | +package azopenaiassistants_test |
| 8 | + |
| 9 | +import ( |
| 10 | + "context" |
| 11 | + "fmt" |
| 12 | + "log" |
| 13 | + "os" |
| 14 | + "time" |
| 15 | + |
| 16 | + "github.com/Azure/azure-sdk-for-go/sdk/ai/azopenaiassistants" |
| 17 | + "github.com/Azure/azure-sdk-for-go/sdk/azcore" |
| 18 | + "github.com/Azure/azure-sdk-for-go/sdk/azcore/to" |
| 19 | +) |
| 20 | + |
| 21 | +func Example_assistantsUsingCodeInterpreter() { |
| 22 | + azureOpenAIKey := os.Getenv("AOAI_ASSISTANTS_KEY") |
| 23 | + |
| 24 | + // Ex: "https://<your-azure-openai-host>.openai.azure.com" |
| 25 | + azureOpenAIEndpoint := os.Getenv("AOAI_ASSISTANTS_ENDPOINT") |
| 26 | + |
| 27 | + if azureOpenAIKey == "" || azureOpenAIEndpoint == "" { |
| 28 | + fmt.Fprintf(os.Stderr, "Skipping example, environment variables missing\n") |
| 29 | + return |
| 30 | + } |
| 31 | + |
| 32 | + keyCredential := azcore.NewKeyCredential(azureOpenAIKey) |
| 33 | + |
| 34 | + client, err := azopenaiassistants.NewClientWithKeyCredential(azureOpenAIEndpoint, keyCredential, nil) |
| 35 | + |
| 36 | + if err != nil { |
| 37 | + // TODO: Update the following line with your application specific error handling logic |
| 38 | + log.Fatalf("ERROR: %s", err) |
| 39 | + } |
| 40 | + |
| 41 | + assistantName := fmt.Sprintf("your-assistant-name-%d", time.Now().UnixNano()) |
| 42 | + |
| 43 | + if err != nil { |
| 44 | + // TODO: Update the following line with your application specific error handling logic |
| 45 | + log.Fatalf("ERROR: %s", err) |
| 46 | + } |
| 47 | + |
| 48 | + // First, let's create an assistant. |
| 49 | + createAssistantResp, err := client.CreateAssistant(context.Background(), azopenaiassistants.AssistantCreationBody{ |
| 50 | + Name: &assistantName, |
| 51 | + DeploymentName: to.Ptr("gpt-4-1106-preview"), |
| 52 | + Instructions: to.Ptr("You are an AI assistant that can write code to help answer math questions."), |
| 53 | + Tools: []azopenaiassistants.ToolDefinitionClassification{ |
| 54 | + &azopenaiassistants.CodeInterpreterToolDefinition{}, |
| 55 | + // others... |
| 56 | + // &azopenaiassistants.FunctionToolDefinition{} |
| 57 | + // &azopenaiassistants.RetrievalToolDefinition{} |
| 58 | + }, |
| 59 | + }, nil) |
| 60 | + |
| 61 | + if err != nil { |
| 62 | + // TODO: Update the following line with your application specific error handling logic |
| 63 | + log.Fatalf("ERROR: %s", err) |
| 64 | + } |
| 65 | + |
| 66 | + assistantID := createAssistantResp.ID |
| 67 | + |
| 68 | + // cleanup the assistant after this example. Remove this if you want to re-use the assistant. |
| 69 | + defer func() { |
| 70 | + _, err := client.DeleteAssistant(context.TODO(), *assistantID, nil) |
| 71 | + |
| 72 | + if err != nil { |
| 73 | + // TODO: Update the following line with your application specific error handling logic |
| 74 | + log.Fatalf("ERROR: %s", err) |
| 75 | + } |
| 76 | + }() |
| 77 | + |
| 78 | + // Now we'll create a thread. The thread is where you will add messages, which can later |
| 79 | + // be evaluated using a Run. A thread can be re-used by multiple Runs. |
| 80 | + createThreadResp, err := client.CreateThread(context.TODO(), azopenaiassistants.AssistantThreadCreationOptions{}, nil) |
| 81 | + |
| 82 | + if err != nil { |
| 83 | + // TODO: Update the following line with your application specific error handling logic |
| 84 | + log.Fatalf("ERROR: %s", err) |
| 85 | + } |
| 86 | + |
| 87 | + threadID := *createThreadResp.ID |
| 88 | + |
| 89 | + // Add a user question to the thread |
| 90 | + ourQuestion, err := client.CreateMessage(context.TODO(), threadID, azopenaiassistants.CreateMessageBody{ |
| 91 | + Content: to.Ptr("I need to solve the equation `3x + 11 = 14`. Can you help me?"), |
| 92 | + Role: to.Ptr(azopenaiassistants.MessageRoleUser), |
| 93 | + }, nil) |
| 94 | + |
| 95 | + if err != nil { |
| 96 | + // TODO: Update the following line with your application specific error handling logic |
| 97 | + log.Fatalf("ERROR: %s", err) |
| 98 | + } |
| 99 | + |
| 100 | + fmt.Fprintf(os.Stderr, "[USER] I need to solve the equation `3x + 11 = 14`. Can you help me?\n") |
| 101 | + |
| 102 | + // Run the thread and wait (using pollRunEnd) until it completes. |
| 103 | + threadRun, err := client.CreateRun(context.TODO(), threadID, azopenaiassistants.CreateRunBody{ |
| 104 | + AssistantID: assistantID, |
| 105 | + }, nil) |
| 106 | + |
| 107 | + if err != nil { |
| 108 | + // TODO: Update the following line with your application specific error handling logic |
| 109 | + log.Fatalf("ERROR: %s", err) |
| 110 | + } |
| 111 | + |
| 112 | + // Wait till the assistant has responded |
| 113 | + if err := pollCodeInterpreterEnd(context.TODO(), client, threadID, *threadRun.ID); err != nil { |
| 114 | + // TODO: Update the following line with your application specific error handling logic |
| 115 | + log.Fatalf("ERROR: %s", err) |
| 116 | + } |
| 117 | + |
| 118 | + // retrieve any messages added after we asked our question. |
| 119 | + listMessagesPager := client.NewListMessagesPager(threadID, &azopenaiassistants.ListMessagesOptions{ |
| 120 | + After: ourQuestion.ID, |
| 121 | + Order: to.Ptr(azopenaiassistants.ListSortOrderAscending), |
| 122 | + }) |
| 123 | + |
| 124 | + for listMessagesPager.More() { |
| 125 | + page, err := listMessagesPager.NextPage(context.Background()) |
| 126 | + |
| 127 | + if err != nil { |
| 128 | + // TODO: Update the following line with your application specific error handling logic |
| 129 | + log.Fatalf("ERROR: %s", err) |
| 130 | + } |
| 131 | + |
| 132 | + for _, threadMessage := range page.Data { |
| 133 | + for _, content := range threadMessage.Content { |
| 134 | + switch v := content.(type) { |
| 135 | + case *azopenaiassistants.MessageTextContent: |
| 136 | + fmt.Fprintf(os.Stderr, "[ASSISTANT] %s: Text response: %s\n", *threadMessage.ID, *v.Text.Value) |
| 137 | + } |
| 138 | + } |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + // Output: |
| 143 | +} |
| 144 | + |
| 145 | +func pollCodeInterpreterEnd(ctx context.Context, client *azopenaiassistants.Client, threadID string, runID string) error { |
| 146 | + for { |
| 147 | + lastGetRunResp, err := client.GetRun(context.Background(), threadID, runID, nil) |
| 148 | + |
| 149 | + if err != nil { |
| 150 | + return err |
| 151 | + } |
| 152 | + |
| 153 | + if *lastGetRunResp.Status != azopenaiassistants.RunStatusQueued && *lastGetRunResp.Status != azopenaiassistants.RunStatusInProgress { |
| 154 | + if *lastGetRunResp.Status == azopenaiassistants.RunStatusCompleted { |
| 155 | + return nil |
| 156 | + } |
| 157 | + |
| 158 | + return fmt.Errorf("run ended but status was not complete: %s", *lastGetRunResp.Status) |
| 159 | + } |
| 160 | + |
| 161 | + select { |
| 162 | + case <-time.After(500 * time.Millisecond): |
| 163 | + case <-ctx.Done(): |
| 164 | + return ctx.Err() |
| 165 | + } |
| 166 | + } |
| 167 | +} |
0 commit comments