1
+ #! /bin/bash
2
+ #
3
+ # Create a .env with the following params
4
+ # AZURE_OPENAI_ENDPOINT
5
+ # AZURE_OPENAI_API_KEY
6
+ # AZURE_OPENAI_DEPLOYMENT_NAME
7
+
8
+ # Load environment variables from .env file
9
+ # Removes trailing whitespace
10
+ export $( grep -v ' ^#' .env | sed ' s/[[:space:]]*$//' | xargs)
11
+
12
+ # Set the REST API version
13
+ AZURE_API_VERSION=2024-08-01-preview
14
+
15
+ ASSISTANT_RESPONSE=$( curl $AZURE_OPENAI_ENDPOINT /openai/assistants? api-version=$AZURE_API_VERSION \
16
+ -H " api-key: $AZURE_OPENAI_API_KEY " \
17
+ -H " Content-Type: application/json" \
18
+ -d ' {
19
+ "instructions": "You are an AI assistant that can write code to help answer math questions.",
20
+ "name": "Math Assist",
21
+ "tools": [{"type": "code_interpreter"}],
22
+ "model": "' $AZURE_OPENAI_DEPLOYMENT_NAME ' "
23
+ }' )
24
+ ASSISTANT_ID=$( echo $ASSISTANT_RESPONSE | jq --raw-output ' .id' )
25
+ echo ' ASSISTANT_ID ' $ASSISTANT_ID
26
+
27
+ # Create a thread
28
+ THREAD_RESPONSE=$( curl $AZURE_OPENAI_ENDPOINT /openai/threads? api-version=$AZURE_API_VERSION \
29
+ -H " Content-Type: application/json" \
30
+ -H " api-key: $AZURE_OPENAI_API_KEY " \
31
+ -d ' ' )
32
+ THREAD_ID=$( echo $THREAD_RESPONSE | jq --raw-output ' .id' )
33
+ echo ' THREAD_ID ' $THREAD_ID
34
+
35
+ # Add a user question to the thread
36
+ MESSAGE_RESPONSE=$( curl $AZURE_OPENAI_ENDPOINT /openai/threads/$THREAD_ID /messages? api-version=$AZURE_API_VERSION \
37
+ -H " Content-Type: application/json" \
38
+ -H " api-key: $AZURE_OPENAI_API_KEY " \
39
+ -d ' {
40
+ "role": "user",
41
+ "content": "I need to solve the equation `3x + 11 = 14`. Can you help me?"
42
+ }' )
43
+ MESSAGE_ID=$( echo $MESSAGE_RESPONSE | jq --raw-output ' .id' )
44
+ echo ' MESSAGE_ID ' $MESSAGE_ID
45
+
46
+ # Run the thread
47
+ RUN_THREAD_RESPONSE=$( curl $AZURE_OPENAI_ENDPOINT /openai/threads/$THREAD_ID /runs? api-version=$AZURE_API_VERSION \
48
+ -H " api-key: $AZURE_OPENAI_API_KEY " \
49
+ -H " Content-Type: application/json" \
50
+ -d ' {
51
+ "assistant_id": "' $ASSISTANT_ID ' "
52
+ }' )
53
+ RUN_ID=$( echo $RUN_THREAD_RESPONSE | jq --raw-output ' .id' )
54
+ echo ' RUN_ID ' $RUN_ID
55
+
56
+ # Retrieve the status of the run - continue until status is completed
57
+ # RUN_STATUS_RESULT=$(curl $AZURE_OPENAI_ENDPOINT/openai/threads/$THREAD_ID/runs/$RUN_ID?api-version=$AZURE_API_VERSION \
58
+ # -H "api-key: $AZURE_OPENAI_API_KEY")
59
+ # RUN_STATUS=$(echo $RUN_STATUS_RESULT | jq --raw-output '.status')
60
+ # echo 'RUN_STATUS ' $RUN_STATUS
61
+
62
+ while true ; do
63
+ # Make the curl request and capture the response
64
+ RUN_STATUS_RESULT=$( curl -s " $AZURE_OPENAI_ENDPOINT /openai/threads/$THREAD_ID /runs/$RUN_ID ?api-version=$AZURE_API_VERSION " \
65
+ -H " api-key: $AZURE_OPENAI_API_KEY " )
66
+
67
+ # Extract the status from the JSON response
68
+ RUN_STATUS=$( echo $RUN_STATUS_RESULT | jq --raw-output ' .status' )
69
+
70
+ # Print the current status
71
+ echo " RUN_STATUS: $RUN_STATUS "
72
+
73
+ # Check if the status is 'completed'
74
+ if [ " $RUN_STATUS " == " completed" ]; then
75
+ break
76
+ fi
77
+
78
+ # Wait for a few seconds before the next iteration
79
+ sleep 5
80
+ done
81
+
82
+ # # Assistant response
83
+ ASSISTANT_ANSWER=$( curl $AZURE_OPENAI_ENDPOINT /openai/threads/$THREAD_ID /messages? api-version=$AZURE_API_VERSION \
84
+ -H " Content-Type: application/json" \
85
+ -H " api-key: $AZURE_OPENAI_API_KEY " )
86
+ # echo 'ASSISTANT_ANSWER ' $ASSISTANT_ANSWER
87
+ echo $ASSISTANT_ANSWER | jq -r ' .data[] | .content[0].text.value'
0 commit comments