Skip to content

Commit

Permalink
Merge pull request #20 from SanduniU/header-sanitation-draft
Browse files Browse the repository at this point in the history
Add the assistant api version header as a sanitation
  • Loading branch information
NipunaRanasinghe authored Aug 22, 2024
2 parents 3098f17 + 2f97d10 commit 59ccfd0
Show file tree
Hide file tree
Showing 14 changed files with 670 additions and 450 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,16 @@ final assistants:Client openAIAssistant = check new ({
Calls to the Assistants API require that you pass a beta HTTP header. In Ballerina, you can define the header as follows:

```ballerina
final map<string|string[]> headers = {
"OpenAI-Beta": ["assistants=v2"]
const record {string OpenAI\-Beta;} headers = {
OpenAI\-Beta: "assistants=v2"
};
```

### Step 3: Invoke the connector operations

Now, utilize the available connector operations.


```ballerina
public function main() returns error? {
Expand All @@ -89,7 +90,7 @@ public function main() returns error? {
};
// call the `post assistants` resource to create an Assistant
assistants:AssistantObject assistantResponse = check openAIAssistant->/assistants.post(request, headers);
assistants:AssistantObject assistant = check openaiAssistant->/assistants.post(headers,request);
}
```

Expand Down
6 changes: 3 additions & 3 deletions ballerina/Module.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ final assistants:Client openAIAssistant = check new ({
Calls to the Assistants API require that you pass a beta HTTP header. In Ballerina, you can define the header as follows:

```ballerina
final map<string|string[]> headers = {
"OpenAI-Beta": ["assistants=v2"]
const record {string OpenAI\-Beta;} headers = {
OpenAI\-Beta: "assistants=v2"
};
```

Expand All @@ -82,7 +82,7 @@ public function main() returns error? {
};
// call the `post assistants` resource to create an Assistant
assistants:AssistantObject assistantResponse = check openAIAssistant->/assistants.post(request, headers);
assistants:AssistantObject assistant = check openaiAssistant->/assistants.post(headers,request);
}
```

Expand Down
6 changes: 3 additions & 3 deletions ballerina/Package.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ final assistants:Client openAIAssistant = check new ({
Calls to the Assistants API require that you pass a beta HTTP header. In Ballerina, you can define the header as follows:

```ballerina
final map<string|string[]> headers = {
"OpenAI-Beta": ["assistants=v2"]
const record {string OpenAI\-Beta;} headers = {
OpenAI\-Beta: "assistants=v2"
};
```

Expand All @@ -81,7 +81,7 @@ public function main() returns error? {
};
// call the `post assistants` resource to create an Assistant
assistants:AssistantObject assistantResponse = check openAIAssistant->/assistants.post(request, headers);
assistants:AssistantObject assistant = check openaiAssistant->/assistants.post(headers,request);
}
```

Expand Down
115 changes: 69 additions & 46 deletions ballerina/client.bal

Large diffs are not rendered by default.

42 changes: 21 additions & 21 deletions ballerina/tests/mock_service.bal
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ http:Service mockService = service object {
#
# + assistant_id - The ID of the assistant to delete.
# + return - OK
resource function delete assistants/[string assistant_id]() returns DeleteAssistantResponse {
resource function delete assistants/[string assistant_id](@http:Header string openai\-beta) returns DeleteAssistantResponse {
DeleteAssistantResponse response = {
"id": "asst_ZDxAT9DQaMD6WACtW1Ap67zE",
"object": "assistant.deleted",
Expand All @@ -77,7 +77,7 @@ http:Service mockService = service object {
#
# + thread_id - The ID of the thread to delete.
# + return - OK
resource function delete threads/[string thread_id]() returns DeleteThreadResponse {
resource function delete threads/[string thread_id](@http:Header string openai\-beta) returns DeleteThreadResponse {
DeleteThreadResponse response = {
"id": "thread_Aui7BIKeelraAZvX7jc6PoLn",
"object": "thread.deleted",
Expand All @@ -91,7 +91,7 @@ http:Service mockService = service object {
# + thread_id - The ID of the thread to which this message belongs.
# + message_id - The ID of the message to delete.
# + return - OK
resource function delete threads/[string thread_id]/messages/[string message_id]() returns DeleteMessageResponse {
resource function delete threads/[string thread_id]/messages/[string message_id](@http:Header string openai\-beta) returns DeleteMessageResponse {
DeleteMessageResponse response = {
"id": "msg_dNdBUSpsQt6bqCWjxi2O7RRo",
"object": "thread.message.deleted",
Expand All @@ -107,7 +107,7 @@ http:Service mockService = service object {
# + after - A cursor for use in pagination. `after` is an object ID that defines your place in the list. For instance, if you make a list request and receive 100 objects, ending with obj_foo, your subsequent call can include after=obj_foo in order to fetch the next page of the list.
# + before - A cursor for use in pagination. `before` is an object ID that defines your place in the list. For instance, if you make a list request and receive 100 objects, ending with obj_foo, your subsequent call can include before=obj_foo in order to fetch the previous page of the list.
# + return - OK
resource function get assistants(string? after, string? before, int 'limit = 20, "asc"|"desc" 'order = "desc") returns ListAssistantsResponse {
resource function get assistants(@http:Header string openai\-beta, string? after, string? before, int 'limit = 20, "asc"|"desc" 'order = "desc") returns ListAssistantsResponse {
ListAssistantsResponse response = {
"object": "list",
"data": [
Expand Down Expand Up @@ -245,7 +245,7 @@ http:Service mockService = service object {
#
# + assistant_id - The ID of the assistant to retrieve.
# + return - OK
resource function get assistants/[string assistant_id]() returns AssistantObject {
resource function get assistants/[string assistant_id](@http:Header string openai\-beta) returns AssistantObject {
AssistantObject assistant = {
"id": "asst_WK0UraQJvxINF81knnBogkrW",
"object": "assistant",
Expand Down Expand Up @@ -276,7 +276,7 @@ http:Service mockService = service object {
#
# + thread_id - The ID of the thread to retrieve.
# + return - OK
resource function get threads/[string thread_id]() returns ThreadObject {
resource function get threads/[string thread_id](@http:Header string openai\-beta) returns ThreadObject {
ThreadObject thread = {
"id": "thread_Aui7BIKeelraAZvX7jc6PoLn",
"object": "thread",
Expand All @@ -300,7 +300,7 @@ http:Service mockService = service object {
# + before - A cursor for use in pagination. `before` is an object ID that defines your place in the list. For instance, if you make a list request and receive 100 objects, ending with obj_foo, your subsequent call can include before=obj_foo in order to fetch the previous page of the list.
# + run_id - Filter messages by the run ID that generated them.
# + return - OK
resource function get threads/[string thread_id]/messages(string? after, string? before, string? run_id, int 'limit = 20, "asc"|"desc" 'order = "desc") returns ListMessagesResponse {
resource function get threads/[string thread_id]/messages(@http:Header string openai\-beta, string? after, string? before, string? run_id, int 'limit = 20, "asc"|"desc" 'order = "desc") returns ListMessagesResponse {
ListMessagesResponse response = {
"object": "list",
"data": [
Expand Down Expand Up @@ -357,7 +357,7 @@ http:Service mockService = service object {
# + thread_id - The ID of the [thread](/docs/api-reference/threads) to which this message belongs.
# + message_id - The ID of the message to retrieve.
# + return - OK
resource function get threads/[string thread_id]/messages/[string message_id]() returns MessageObject {
resource function get threads/[string thread_id]/messages/[string message_id](@http:Header string openai\-beta) returns MessageObject {
MessageObject message = {
"id": "msg_PBtjbgfLc82Cez4AMm6u72HI",
"object": "thread.message",
Expand Down Expand Up @@ -389,7 +389,7 @@ http:Service mockService = service object {
# + after - A cursor for use in pagination. `after` is an object ID that defines your place in the list. For instance, if you make a list request and receive 100 objects, ending with obj_foo, your subsequent call can include after=obj_foo in order to fetch the next page of the list.
# + before - A cursor for use in pagination. `before` is an object ID that defines your place in the list. For instance, if you make a list request and receive 100 objects, ending with obj_foo, your subsequent call can include before=obj_foo in order to fetch the previous page of the list.
# + return - OK
resource function get threads/[string thread_id]/runs(string? after, string? before, int 'limit = 20, "asc"|"desc" 'order = "desc") returns ListRunsResponse {
resource function get threads/[string thread_id]/runs(@http:Header string openai\-beta, string? after, string? before, int 'limit = 20, "asc"|"desc" 'order = "desc") returns ListRunsResponse {
ListRunsResponse response = {
"object": "list",
"data": [
Expand Down Expand Up @@ -488,7 +488,7 @@ http:Service mockService = service object {
# + thread_id - The ID of the [thread](/docs/api-reference/threads) that was run.
# + run_id - The ID of the run to retrieve.
# + return - OK
resource function get threads/[string thread_id]/runs/[string run_id]() returns RunObject|error {
resource function get threads/[string thread_id]/runs/[string run_id](@http:Header string openai\-beta) returns RunObject {
RunObject run = {
"id": "run_c73jgpUPMts5wIsC2jOwqYyk",
"object": "thread.run",
Expand Down Expand Up @@ -542,7 +542,7 @@ http:Service mockService = service object {
# + after - A cursor for use in pagination. `after` is an object ID that defines your place in the list. For instance, if you make a list request and receive 100 objects, ending with obj_foo, your subsequent call can include after=obj_foo in order to fetch the next page of the list.
# + before - A cursor for use in pagination. `before` is an object ID that defines your place in the list. For instance, if you make a list request and receive 100 objects, ending with obj_foo, your subsequent call can include before=obj_foo in order to fetch the previous page of the list.
# + return - OK
resource function get threads/[string thread_id]/runs/[string run_id]/steps(string? after, string? before, int 'limit = 20, "asc"|"desc" 'order = "desc") returns ListRunStepsResponse {
resource function get threads/[string thread_id]/runs/[string run_id]/steps(@http:Header string openai\-beta, string? after, string? before, int 'limit = 20, "asc"|"desc" 'order = "desc") returns ListRunStepsResponse {
ListRunStepsResponse response = {
"object": "list",
"data": [
Expand Down Expand Up @@ -586,7 +586,7 @@ http:Service mockService = service object {
# + run_id - The ID of the run to which the run step belongs.
# + step_id - The ID of the run step to retrieve.
# + return - OK
resource function get threads/[string thread_id]/runs/[string run_id]/steps/[string step_id]() returns RunStepObject {
resource function get threads/[string thread_id]/runs/[string run_id]/steps/[string step_id](@http:Header string openai\-beta) returns RunStepObject {
RunStepObject runStep = {
"id": "step_VJjXo2AeyYxsprKooW7yCnCF",
"object": "thread.run.step",
Expand Down Expand Up @@ -619,7 +619,7 @@ http:Service mockService = service object {
# Create an assistant with a model and instructions.
#
# + return - OK
resource function post assistants(@http:Payload CreateAssistantRequest payload) returns OkAssistantObject {
resource function post assistants(@http:Header string openai\-beta, @http:Payload CreateAssistantRequest payload) returns OkAssistantObject {
AssistantObject assistant = {
"id": "asst_WK0UraQJvxINF81knnBogkrW",
"object": "assistant",
Expand Down Expand Up @@ -657,7 +657,7 @@ http:Service mockService = service object {
#
# + assistant_id - The ID of the assistant to modify.
# + return - OK
resource function post assistants/[string assistant_id](@http:Payload ModifyAssistantRequest payload) returns OkAssistantObject {
resource function post assistants/[string assistant_id](@http:Header string openai\-beta, @http:Payload ModifyAssistantRequest payload) returns OkAssistantObject {
AssistantObject assistant = {
"id": "asst_WK0UraQJvxINF81knnBogkrW",
"object": "assistant",
Expand Down Expand Up @@ -693,7 +693,7 @@ http:Service mockService = service object {
# Create a thread.
#
# + return - OK
resource function post threads(@http:Payload CreateThreadRequest payload) returns OkThreadObject {
resource function post threads(@http:Header string openai\-beta, @http:Payload CreateThreadRequest payload) returns OkThreadObject {
ThreadObject thread = {
"id": "thread_Aui7BIKeelraAZvX7jc6PoLn",
"object": "thread",
Expand All @@ -714,7 +714,7 @@ http:Service mockService = service object {
#
# + thread_id - The ID of the thread to modify. Only the `metadata` can be modified.
# + return - OK
resource function post threads/[string thread_id](@http:Payload ModifyThreadRequest payload) returns OkThreadObject {
resource function post threads/[string thread_id](@http:Header string openai\-beta, @http:Payload ModifyThreadRequest payload) returns OkThreadObject {
ThreadObject thread = {
"id": "thread_Aui7BIKeelraAZvX7jc6PoLn",
"object": "thread",
Expand All @@ -739,7 +739,7 @@ http:Service mockService = service object {
#
# + thread_id - The ID of the [thread](/docs/api-reference/threads) to create a message for.
# + return - OK
resource function post threads/[string thread_id]/messages(@http:Payload CreateMessageRequest payload) returns OkMessageObject {
resource function post threads/[string thread_id]/messages(@http:Header string openai\-beta, @http:Payload CreateMessageRequest payload) returns OkMessageObject {
MessageObject message = {
"id": "msg_PBtjbgfLc82Cez4AMm6u72HI",
"object": "thread.message",
Expand Down Expand Up @@ -775,7 +775,7 @@ http:Service mockService = service object {
# + thread_id - The ID of the thread to which this message belongs.
# + message_id - The ID of the message to modify.
# + return - OK
resource function post threads/[string thread_id]/messages/[string message_id](@http:Payload ModifyMessageRequest payload) returns OkMessageObject {
resource function post threads/[string thread_id]/messages/[string message_id](@http:Header string openai\-beta, @http:Payload ModifyMessageRequest payload) returns OkMessageObject {
MessageObject message = {
"id": "msg_dNdBUSpsQt6bqCWjxi2O7RRo",
"object": "thread.message",
Expand Down Expand Up @@ -809,7 +809,7 @@ http:Service mockService = service object {
#
# + thread_id - The ID of the thread to run.
# + return - OK
resource function post threads/[string thread_id]/runs(@http:Payload CreateRunRequest payload) returns OkRunObject {
resource function post threads/[string thread_id]/runs(@http:Header string openai\-beta, @http:Payload CreateRunRequest payload) returns OkRunObject {
RunObject run = {
"id": "run_c73jgpUPMts5wIsC2jOwqYyk",
"object": "thread.run",
Expand Down Expand Up @@ -861,7 +861,7 @@ http:Service mockService = service object {
# + thread_id - The ID of the [thread](/docs/api-reference/threads) that was run.
# + run_id - The ID of the run to modify.
# + return - OK
resource function post threads/[string thread_id]/runs/[string run_id](@http:Payload ModifyRunRequest payload) returns OkRunObject {
resource function post threads/[string thread_id]/runs/[string run_id](@http:Header string openai\-beta, @http:Payload ModifyRunRequest payload) returns OkRunObject {
RunObject run = {
"id": "run_c73jgpUPMts5wIsC2jOwqYyk",
"object": "thread.run",
Expand Down Expand Up @@ -931,7 +931,7 @@ http:Service mockService = service object {
# Create a thread and run it in one request.
#
# + return - OK
resource function post threads/runs(@http:Payload CreateThreadAndRunRequest payload) returns OkRunObject {
resource function post threads/runs(@http:Header string openai\-beta, @http:Payload CreateThreadAndRunRequest payload) returns OkRunObject {
RunObject run = {
"id": "run_c73jgpUPMts5wIsC2jOwqYyk",
"object": "thread.run",
Expand Down
17 changes: 9 additions & 8 deletions ballerina/tests/test.bal
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ string messageId = "";
string runId = "";
string stepId = "";

const map<string|string[]> headers = {
"OpenAI-Beta": ["assistants=v2"]
const record {string OpenAI\-Beta;} headers = {
OpenAI\-Beta: "assistants=v2"
};

@test:Config {
Expand Down Expand Up @@ -78,7 +78,7 @@ function testCreateMessage() returns error? {
content: "Can you help me solve the equation `3x + 11 = 14`?"
};

MessageObject res = check openAIAssistant->/threads/[threadId]/messages.post(createMsgReq, headers);
MessageObject res = check openAIAssistant->/threads/[threadId]/messages.post(headers, createMsgReq);
test:assertNotEquals(res.id, "");
messageId = res.id;
}
Expand Down Expand Up @@ -111,7 +111,7 @@ function testCreateRun() returns error? {
max_completion_tokens: 200
};

RunObject resp = check openAIAssistant->/threads/[threadId]/runs.post(runReq, headers);
RunObject resp = check openAIAssistant->/threads/[threadId]/runs.post(headers, runReq);
test:assertNotEquals(resp.id, "", msg = "Run creation failed: No Run ID returned");
runId = resp.id;
}
Expand Down Expand Up @@ -140,7 +140,7 @@ function testCreateThreadAndRun() returns error? {
// max_completion_tokens: 200
// };

// RunObject resp = check openAIAssistant->/threads/runs.post(createThreadAndRunReq, headers);
// RunObject resp = check openAIAssistant->/threads/runs.post(headers,createThreadAndRunReq);
// test:assertNotEquals(resp.id, "", msg = "Thread and Run creation failed: No Run ID returned");
}

Expand All @@ -152,7 +152,7 @@ function testCreateThread() returns error? {
messages: []
};

ThreadObject response = check openAIAssistant->/threads.post(createThreadReq, headers);
ThreadObject response = check openAIAssistant->/threads.post(headers, createThreadReq);
threadId = response.id;
test:assertNotEquals(response.id, "");
}
Expand Down Expand Up @@ -201,15 +201,15 @@ function testCreateAssistant() returns error? {
'type: "code_interpreter"
};

CreateAssistantRequest request = {
CreateAssistantRequest createAssistantReq = {
model: "gpt-3.5-turbo",
name: "Math Tutor",
description: "An Assistant for personal math tutoring",
instructions: "You are a personal math tutor. Help the user with their math questions.",
tools: [codeTool]
};

AssistantObject res = check openAIAssistant->/assistants.post(request, headers);
AssistantObject res = check openAIAssistant->/assistants.post(headers, createAssistantReq);
assistantId = res.id;
test:assertNotEquals(res.id, "");
}
Expand All @@ -233,6 +233,7 @@ function testListAssistants() returns error? {
groups: ["live_tests", "mock_tests"]
}
function testGetRunStep() returns error? {

if stepId == "" {
test:assertEquals(stepId, "", msg = "No step ID available.");
}
Expand Down
Loading

0 comments on commit 59ccfd0

Please sign in to comment.