Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add documentation #4

Merged
merged 18 commits into from
Aug 14, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 0 additions & 72 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,78 +100,6 @@ public function main() returns error? {
}
```

#### Create a thread
NipunaRanasinghe marked this conversation as resolved.
Show resolved Hide resolved

A Thread represents a conversation between a user and one or many Assistants. You can create a Thread when a user (or your AI application) starts a conversation with your Assistant.

```ballerina
public function main() returns error? {
// define the thread request
assistants:CreateThreadRequest createThreadReq = {
messages: []
};

// call the `post threads` resource to create a Thread
assistants:ThreadObject threadResponse = check openAIAssistant->/threads.post(createThreadReq, headers);
}
```

#### Add a message to the thread

The contents of the messages your users or applications create are added as Message objects to the Thread. Messages can contain both text and files. There is no limit to the number of Messages you can add to Threads — the context that does not fit into the model's context window will be truncated automatically.

```ballerina
public function main() returns error? {
string threadId = "your_thread_id";

// define the message object
assistants:CreateMessageRequest createMsgReq = {
role: "user",
content: "Can you help me solve the equation `3x + 11 = 14`?",
metadata: {}
};

// create a message in the thread
assistants:MessageObject messageResponse = check openAIAssistant->/threads/[threadId]/messages.post(createMsgReq, headers);
}
```

#### Create a run

Once all the user Messages have been added to the Thread, you can Run the Thread with any Assistant. Creating a Run uses the model and tools associated with the Assistant to generate a response. These responses are added to the Thread as Assistant Messages.

```ballerina
public function main() returns error? {
string threadId = "your_thread_id";

// define the run request object
assistants:CreateRunRequest runReq = {
assistant_id: "your_assistant_id",
model: "gpt-3.5-turbo",
instructions: "You are a personal math tutor. Assist the user with their math questions.",
temperature: 0.7,
top_p: 0.9,
max_prompt_tokens: 400,
max_completion_tokens: 200
};

// create a run in the thread
assistants:RunObject runResponse = check openAIAssistant->/threads/[threadId]/runs.post(runReq, headers);

}
```
Once the Run completes, you can list the Messages added to the Thread by the Assistant.

```ballerina
public function main() returns error? {
string threadId = "your_thread_id";

// list messages in the thread
assistants:ListMessagesResponse listResponse = check openAIAssistant->/threads/[threadId]/messages.get(headers);
}

```

### Step 4: Run the Ballerina application

```bash
Expand Down
72 changes: 0 additions & 72 deletions ballerina/Module.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,78 +92,6 @@ public function main() returns error? {
}
```

#### Create a thread

A Thread represents a conversation between a user and one or many Assistants. You can create a Thread when a user (or your AI application) starts a conversation with your Assistant.

```ballerina
public function main() returns error? {
// define the thread request
assistants:CreateThreadRequest createThreadReq = {
messages: []
};

// call the `post threads` resource to create a Thread
assistants:ThreadObject threadResponse = check openAIAssistant->/threads.post(createThreadReq, headers);
}
```

#### Add a message to the thread

The contents of the messages your users or applications create are added as Message objects to the Thread. Messages can contain both text and files. There is no limit to the number of Messages you can add to Threads — the context that does not fit into the model's context window will be truncated automatically.

```ballerina
public function main() returns error? {
string threadId = "your_thread_id";

// define the message object
assistants:CreateMessageRequest createMsgReq = {
role: "user",
content: "Can you help me solve the equation `3x + 11 = 14`?",
metadata: {}
};

// create a message in the thread
assistants:MessageObject messageResponse = check openAIAssistant->/threads/[threadId]/messages.post(createMsgReq, headers);
}
```

#### Create a run

Once all the user Messages have been added to the Thread, you can Run the Thread with any Assistant. Creating a Run uses the model and tools associated with the Assistant to generate a response. These responses are added to the Thread as Assistant Messages.

```ballerina
public function main() returns error? {
string threadId = "your_thread_id";

// define the run request object
assistants:CreateRunRequest runReq = {
assistant_id: "your_assistant_id",
model: "gpt-3.5-turbo",
instructions: "You are a personal math tutor. Assist the user with their math questions.",
temperature: 0.7,
top_p: 0.9,
max_prompt_tokens: 400,
max_completion_tokens: 200
};

// create a run in the thread
assistants:RunObject runResponse = check openAIAssistant->/threads/[threadId]/runs.post(runReq, headers);

}
```
Once the Run completes, you can list the Messages added to the Thread by the Assistant.

```ballerina
public function main() returns error? {
string threadId = "your_thread_id";

// list messages in the thread
assistants:ListMessagesResponse listResponse = check openAIAssistant->/threads/[threadId]/messages.get(headers);
}

```

### Step 4: Run the Ballerina application

```bash
Expand Down
74 changes: 0 additions & 74 deletions ballerina/Package.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ OpenAI is an American artificial intelligence research organization that compris

The `ballarinax/openai.Assistants` connector allows developers to seamlessly integrate OpenAI's advanced language models into their applications by interacting with [OpenAI REST API v1](https://platform.openai.com/docs/api-reference/assistants). This connector provides tools to build powerful [OpenAI Assistants](https://platform.openai.com/docs/assistants/overview) capable of performing a wide range of tasks, such as generating human-like text, managing conversations with persistent threads, and utilizing multiple tools in parallel. OpenAI has recently announced a variety of new features and improvements to the Assistants API, moving their Beta to a [new API version](https://platform.openai.com/docs/assistants/whats-new), `OpenAI-Beta: assistants=v2`. The users can interact with both the API v1 and v2 by [passing the respective API version header in the request](https://platform.openai.com/docs/assistants/migration/changing-beta-versions)


## Setup guide

To use the OpenAI Connector, you must have access to the OpenAI API through a [OpenAI Platform account](https://platform.openai.com) and a project under it. If you do not have a OpenAI Platform account, you can sign up for one [here](https://platform.openai.com/signup).
Expand All @@ -13,7 +12,6 @@ To use the OpenAI Connector, you must have access to the OpenAI API through a [O

1. Open the [OpenAI Platform Dashboard](https://platform.openai.com).


2. Navigate to Dashboard -> API keys
<img src=https://github.com/user-attachments/assets/b2e09c6d-c15f-4cfa-a596-6328b1383162 alt="OpenAI Platform" style="width: 70%;">

Expand Down Expand Up @@ -92,78 +90,6 @@ public function main() returns error? {
}
```

#### Create a thread

A Thread represents a conversation between a user and one or many Assistants. You can create a Thread when a user (or your AI application) starts a conversation with your Assistant.

```ballerina
public function main() returns error? {
// define the thread request
assistants:CreateThreadRequest createThreadReq = {
messages: []
};

// call the `post threads` resource to create a Thread
assistants:ThreadObject threadResponse = check openAIAssistant->/threads.post(createThreadReq, headers);
}
```

#### Add a message to the thread

The contents of the messages your users or applications create are added as Message objects to the Thread. Messages can contain both text and files. There is no limit to the number of Messages you can add to Threads — the context that does not fit into the model's context window will be truncated automatically.

```ballerina
public function main() returns error? {
string threadId = "your_thread_id";

// define the message object
assistants:CreateMessageRequest createMsgReq = {
role: "user",
content: "Can you help me solve the equation `3x + 11 = 14`?",
metadata: {}
};

// create a message in the thread
assistants:MessageObject messageResponse = check openAIAssistant->/threads/[threadId]/messages.post(createMsgReq, headers);
}
```

#### Create a run

Once all the user Messages have been added to the Thread, you can Run the Thread with any Assistant. Creating a Run uses the model and tools associated with the Assistant to generate a response. These responses are added to the Thread as Assistant Messages.

```ballerina
public function main() returns error? {
string threadId = "your_thread_id";

// define the run request object
assistants:CreateRunRequest runReq = {
assistant_id: "your_assistant_id",
model: "gpt-3.5-turbo",
instructions: "You are a personal math tutor. Assist the user with their math questions.",
temperature: 0.7,
top_p: 0.9,
max_prompt_tokens: 400,
max_completion_tokens: 200
};

// create a run in the thread
assistants:RunObject runResponse = check openAIAssistant->/threads/[threadId]/runs.post(runReq, headers);

}
```
Once the Run completes, you can list the Messages added to the Thread by the Assistant.

```ballerina
public function main() returns error? {
string threadId = "your_thread_id";

// list messages in the thread
assistants:ListMessagesResponse listResponse = check openAIAssistant->/threads/[threadId]/messages.get(headers);
}

```

### Step 4: Run the Ballerina application

```bash
Expand Down
Loading