Skip to content

Commit

Permalink
[azopenaiassistants] Updated changelog, added in examples for more co…
Browse files Browse the repository at this point in the history
…mplex scenarios. (#23106)

* Updated changelog, added in examples for more complex scenarios.
* Noticed I was using context.Background() sometimes in examples, but it should always be context.TODO()
  • Loading branch information
richardpark-msft authored Jun 25, 2024
1 parent d765680 commit 1798e12
Show file tree
Hide file tree
Showing 7 changed files with 301 additions and 25 deletions.
6 changes: 2 additions & 4 deletions sdk/ai/azopenaiassistants/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
# Release History

## 0.2.0 (TBD)
## 0.2.0 (2024-06-25)

### Features Added

- Now supports the Assistants V2 API, with support for vector stores.

### Bugs Fixed
- Now supports the Assistants V2 API, with support for vector stores as well as streaming.

### Breaking Changes

Expand Down
2 changes: 1 addition & 1 deletion sdk/ai/azopenaiassistants/client_custom_streaming_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ func submitToolOutputsWithStreaming(t *testing.T, client *azopenaiassistants.Cli
resp, err := client.SubmitToolOutputsToRunStream(context.Background(), *lastResp.ThreadID, *lastResp.ID, azopenaiassistants.SubmitToolOutputsToRunBody{
ToolOutputs: []azopenaiassistants.ToolOutput{
{
Output: to.Ptr("0C"),
Output: to.Ptr("26C"),
ToolCallID: funcToolCall.ID,
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func Example_assistantsUsingCodeInterpreter() {
assistantName := fmt.Sprintf("your-assistant-name-%d", time.Now().UnixNano())

// First, let's create an assistant.
createAssistantResp, err := client.CreateAssistant(context.Background(), azopenaiassistants.CreateAssistantBody{
createAssistantResp, err := client.CreateAssistant(context.TODO(), azopenaiassistants.CreateAssistantBody{
Name: &assistantName,
DeploymentName: to.Ptr("gpt-4-1106-preview"),
Instructions: to.Ptr("You are an AI assistant that can write code to help answer math questions."),
Expand Down Expand Up @@ -122,7 +122,7 @@ func Example_assistantsUsingCodeInterpreter() {
})

for listMessagesPager.More() {
page, err := listMessagesPager.NextPage(context.Background())
page, err := listMessagesPager.NextPage(context.TODO())

if err != nil {
// TODO: Update the following line with your application specific error handling logic
Expand All @@ -143,7 +143,7 @@ func Example_assistantsUsingCodeInterpreter() {

func pollCodeInterpreterEnd(ctx context.Context, client *azopenaiassistants.Client, threadID string, runID string) (azopenaiassistants.GetRunResponse, error) {
for {
lastGetRunResp, err := client.GetRun(context.Background(), threadID, runID, nil)
lastGetRunResp, err := client.GetRun(context.TODO(), threadID, runID, nil)

if err != nil {
return azopenaiassistants.GetRunResponse{}, err
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func Example_assistantsWithConversationLoop() {
}

// First, let's create an assistant.
createAssistantResp, err := client.CreateAssistant(context.Background(), azopenaiassistants.CreateAssistantBody{
createAssistantResp, err := client.CreateAssistant(context.TODO(), azopenaiassistants.CreateAssistantBody{
Name: &assistantName,
DeploymentName: to.Ptr("gpt-4-1106-preview"),
Instructions: to.Ptr("You are a personal math tutor. Write and run code to answer math questions."),
Expand Down Expand Up @@ -79,7 +79,7 @@ func Example_assistantsWithConversationLoop() {

// Now we'll create a thread. The thread is where you will add messages, which can later
// be evaluated using a Run. A thread can be re-used by multiple Runs.
createThreadResp, err := client.CreateThread(context.Background(), azopenaiassistants.CreateThreadBody{}, nil)
createThreadResp, err := client.CreateThread(context.TODO(), azopenaiassistants.CreateThreadBody{}, nil)

if err != nil {
// TODO: Update the following line with your application specific error handling logic
Expand Down Expand Up @@ -172,7 +172,7 @@ func assistantLoop(ctx context.Context, client *azopenaiassistants.Client,
for _, yourResponse := range yourResponses {
// Add some messages to the thread. We will use Run the thread later to evaluate these and to get
// responses from the assistant.
createMessageResp, err := client.CreateMessage(context.Background(), threadID, yourResponse, nil)
createMessageResp, err := client.CreateMessage(context.TODO(), threadID, yourResponse, nil)

if err != nil {
return err
Expand All @@ -183,7 +183,7 @@ func assistantLoop(ctx context.Context, client *azopenaiassistants.Client,
lastMessageID = createMessageResp.ID
}

createRunResp, err := client.CreateRun(context.Background(), threadID, azopenaiassistants.CreateRunBody{
createRunResp, err := client.CreateRun(context.TODO(), threadID, azopenaiassistants.CreateRunBody{
AssistantID: &assistantID,
}, nil)

Expand Down Expand Up @@ -239,7 +239,7 @@ func printAssistantMessages(ctx context.Context, client *azopenaiassistants.Clie

func pollUntilRunEnds(ctx context.Context, client *azopenaiassistants.Client, threadID string, runID string) (azopenaiassistants.GetRunResponse, error) {
for {
lastGetRunResp, err := client.GetRun(context.Background(), threadID, runID, nil)
lastGetRunResp, err := client.GetRun(context.TODO(), threadID, runID, nil)

if err != nil {
return azopenaiassistants.GetRunResponse{}, err
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func Example_assistantsUsingFunctionTool() {
}

// First, let's create an assistant.
createAssistantResp, err := client.CreateAssistant(context.Background(), azopenaiassistants.CreateAssistantBody{
createAssistantResp, err := client.CreateAssistant(context.TODO(), azopenaiassistants.CreateAssistantBody{
Name: &assistantName,
DeploymentName: to.Ptr("gpt-4-1106-preview"),
Instructions: to.Ptr("You are a personal math tutor. Write and run code to answer math questions."),
Expand All @@ -90,7 +90,7 @@ func Example_assistantsUsingFunctionTool() {
var lastMessageID, runID, threadID string
{
fmt.Fprintf(os.Stderr, "Creating our thread\n")
createThreadResp, err := client.CreateThread(context.Background(), azopenaiassistants.CreateThreadBody{}, nil)
createThreadResp, err := client.CreateThread(context.TODO(), azopenaiassistants.CreateThreadBody{}, nil)

if err != nil {
// TODO: Update the following line with your application specific error handling logic
Expand All @@ -99,7 +99,7 @@ func Example_assistantsUsingFunctionTool() {

threadID = *createThreadResp.ID

msgResponse, err := client.CreateMessage(context.Background(), threadID, azopenaiassistants.CreateMessageBody{
msgResponse, err := client.CreateMessage(context.TODO(), threadID, azopenaiassistants.CreateMessageBody{
Content: &question,
Role: to.Ptr(azopenaiassistants.MessageRoleUser),
}, nil)
Expand All @@ -114,7 +114,7 @@ func Example_assistantsUsingFunctionTool() {
// run the thread
fmt.Fprintf(os.Stderr, "Creating our run for thread %s\n", *createThreadResp.ID)

createRunResp, err := client.CreateRun(context.Background(), *createThreadResp.ID, azopenaiassistants.CreateRunBody{
createRunResp, err := client.CreateRun(context.TODO(), *createThreadResp.ID, azopenaiassistants.CreateRunBody{
AssistantID: createAssistantResp.ID,
Instructions: to.Ptr("Use functions to answer questions, when possible."),
}, nil)
Expand All @@ -130,7 +130,7 @@ func Example_assistantsUsingFunctionTool() {
fmt.Fprintf(os.Stderr, "Waiting for the Run status to indicate it needs tool outputs\n")
// NOTE: see https://pkg.go.dev/github.com/Azure/azure-sdk-for-go/sdk/ai/azopenaiassistants#example-package-AssistantsConversationLoop
// for the pollUntilRunEnds function.
lastResp, err := pollUntilRunEnds(context.Background(), client, threadID, runID)
lastResp, err := pollUntilRunEnds(context.TODO(), client, threadID, runID)
if err != nil {
// TODO: Update the following line with your application specific error handling logic
log.Fatalf("ERROR: %s", err)
Expand Down Expand Up @@ -171,7 +171,7 @@ func Example_assistantsUsingFunctionTool() {
fmt.Fprintf(os.Stderr, "Call our own function to get the weather for %s, in %s\n", weatherFuncArgs.Location, weatherFuncArgs.Unit)
{
// submit our outputs from evaluating the tool
_, err := client.SubmitToolOutputsToRun(context.Background(), threadID, runID, azopenaiassistants.SubmitToolOutputsToRunBody{
_, err := client.SubmitToolOutputsToRun(context.TODO(), threadID, runID, azopenaiassistants.SubmitToolOutputsToRunBody{
ToolOutputs: []azopenaiassistants.ToolOutput{
{
Output: to.Ptr("0C"),
Expand All @@ -189,7 +189,7 @@ func Example_assistantsUsingFunctionTool() {
// the run will restart now, we just need to wait until it finishes
// NOTE: see https://pkg.go.dev/github.com/Azure/azure-sdk-for-go/sdk/ai/azopenaiassistants#example-package-AssistantsConversationLoop
// for the pollUntilRunEnds function.
lastResp, err = pollUntilRunEnds(context.Background(), client, threadID, runID)
lastResp, err = pollUntilRunEnds(context.TODO(), client, threadID, runID)

if err != nil || *lastResp.Status != azopenaiassistants.RunStatusCompleted {
// TODO: Update the following line with your application specific error handling logic
Expand All @@ -200,7 +200,7 @@ func Example_assistantsUsingFunctionTool() {

// NOTE: see https://pkg.go.dev/github.com/Azure/azure-sdk-for-go/sdk/ai/azopenaiassistants#example-package-AssistantsConversationLoop
// for the getLatestMessages function.
latestMessages, err := getLatestMessages(context.Background(), client, threadID, &lastMessageID)
latestMessages, err := getLatestMessages(context.TODO(), client, threadID, &lastMessageID)
if err != nil {
// TODO: Update the following line with your application specific error handling logic
log.Fatalf("ERROR: %s", err)
Expand All @@ -211,7 +211,7 @@ func Example_assistantsUsingFunctionTool() {
// [ASSISTANT] <id>: Text response: The current weather in Boston, MA, measured in Celsius, is 0°C.
// NOTE: see https://pkg.go.dev/github.com/Azure/azure-sdk-for-go/sdk/ai/azopenaiassistants#example-package-AssistantsConversationLoop
// for the printAssistantMessages function.
err = printAssistantMessages(context.Background(), client, latestMessages)
err = printAssistantMessages(context.TODO(), client, latestMessages)

if err != nil {
// TODO: Update the following line with your application specific error handling logic
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func Example_assistantsStreaming() {
assistantName := fmt.Sprintf("your-assistant-name-%d", time.Now().UnixNano())

// First, let's create an assistant.
createAssistantResp, err := client.CreateAssistant(context.Background(), azopenaiassistants.CreateAssistantBody{
createAssistantResp, err := client.CreateAssistant(context.TODO(), azopenaiassistants.CreateAssistantBody{
Name: &assistantName,
DeploymentName: to.Ptr("gpt-4-1106-preview"),
Instructions: to.Ptr("You are an AI assistant that can write code to help answer math questions."),
Expand Down Expand Up @@ -73,7 +73,7 @@ func Example_assistantsStreaming() {
}
}()

resp, err := client.CreateThreadAndRunStream(context.Background(), azopenaiassistants.CreateAndRunThreadBody{
resp, err := client.CreateThreadAndRunStream(context.TODO(), azopenaiassistants.CreateAndRunThreadBody{
AssistantID: assistantID,
DeploymentName: &assistantsModel,
Instructions: to.Ptr("You're a helpful assistant that provides answers on questions about beetles."),
Expand Down Expand Up @@ -109,7 +109,7 @@ func Example_assistantsStreaming() {
}

// NOTE: for this example we're handling a small subset of the events that are
// streaming in. See [AssistantStreamEvent] for the full list of available events.
// streaming in. See [azopenaiassistants.AssistantStreamEvent] for the full list of available events.
switch event.Reason {
// Assistant events
case azopenaiassistants.AssistantStreamEventThreadCreated:
Expand Down
Loading

0 comments on commit 1798e12

Please sign in to comment.