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 3 commits
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
42 changes: 38 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Compiled class file
*.class

*.balx
# Log file
*.log

Expand All @@ -10,15 +10,49 @@
# Mobile Tools for Java (J2ME)
.mtj.tmp/

# Package Files #
# .DS_Store files
*.DS_Store

# Package Files
*.jar
!gradle/wrapper/gradle-wrapper.jar
*.war
*.nar
*.ear
*.zip
*.tar.gz
*.rar
*.deb

# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*
replay_pid*

# Ignore everything in this directory
target
.classpath
.settings
.project
*.iml
*.iws
*.ipr
.idea
.m2
.vscode/

# Ignore ballerina files
accessToken.bal
temp.bal.ballerina/
target/
generated/
.DS_Store
*Ballerina.lock
.ballerina
**/Config.toml

# Ignore Gradle project-specific cache directory
.gradle

# Ignore Gradle build output directory
build

# Ignore Docker env file
docker.env
208 changes: 205 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,217 @@

## Overview

[//]: # (TODO: Add overview mentioning the purpose of the module, supported REST API versions, and other high-level details.)
OpenAI is an American artificial intelligence research organization that comprises both a non-profit and a for-profit entity. The organization focuses on conducting cutting-edge AI research with the goal of developing friendly AI that benefits humanity. By advancing the state of AI, OpenAI aims to ensure that powerful AI technologies are used responsibly and ethically, promoting innovation while addressing potential risks.

The [OpenAI Assistants API](https://platform.openai.com/docs/api-reference/assistants) Connector allows developers to seamlessly integrate OpenAI's advanced language models into their applications. 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`.


## Setup guide

[//]: # (TODO: Add detailed steps to obtain credentials and configure the module.)
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).

#### Create a OpenAI API Key

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%;">


3. Click on the "Create new secret key" button
<img src=https://github.com/user-attachments/assets/bf1adab4-5e3f-4094-9a56-5b4c3cc3c19e alt="OpenAI Platform" style="width: 70%;">


4. Fill the details and click on Create secret key
<img src=https://github.com/user-attachments/assets/1c565923-e968-4d5f-9864-7ed2022b8079 alt="OpenAI Platform" style="width: 70%;">


5. Store the API key securely to use in your application
<img src=https://github.com/user-attachments/assets/bbbf8f38-d551-40ee-9664-f4cf2bd98997 alt="OpenAI Platform" style="width: 70%;">



## Quickstart

[//]: # (TODO: Add a quickstart guide to demonstrate a basic functionality of the module, including sample code snippets.)
A typical integration of the Assistants API has the following flow:

1. **Create an Assistant**
- Define its custom instructions and pick a model.
- If helpful, add files and enable tools like Code Interpreter, File Search, and Function calling.

2. **Create a Thread**
- Create a Thread when a user starts a conversation.

3. **Add Messages to the Thread**
- Add Messages to the Thread as the user asks questions.

4. **Run the Assistant**
- Run the Assistant on the Thread to generate a response by calling the model and the tools.

This starter guide walks through the key steps to create and run an Assistant that uses the Code Interpreter tool. In this example, we're creating an Assistant that is a personal math tutor.
### Setting HTTP Headers in Ballerina

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"]
};
```

### Step 1 : Setting up the connector
To use the `OpenAI Assistants` connector in your Ballerina application, update the `.bal` file as follows:

1. Import the `openai_assistants` module.

```ballerina
import ballerinax/openai_assistants;
```

2. Create a `Config.toml` file and configure the obtained credentials as follows:

```bash
token = "<Access Token>"
```

3. Create a `openai_assistants:Client` with the obtained access token and initialize the connector with it.

```ballerina
configurable string token = ?;

final openai_assistants:Client AssistantClient = check new({
auth: {
token
}
});
```

### Step 2: Create an Assistant

Now, utilize the available connector operations to create an Assistant.



```ballerina
public function main() returns error? {

// define the required tool
openai_assistants:AssistantToolsCode tool = {
type: "code_interpreter"
};

// define the assistant request object
openai_assistants:CreateAssistantRequest request = {
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: [tool]
};

// Call the `post assistants` resource to create an Assistant
var response = check AssistantClient->/assistants.post(request, headers);
io:println("Assistant ID: ", response);

if (response is openai_assistants:AssistantObject) {
io:println("Assistant created: ", response);
} else {
io:println("Error: ", response);
}
}
```

### Step 3: 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?{
openai_assistants:CreateThreadRequest createThreadReq = {
messages: []
};

// Call the `post threads` resource to create a Thread
var threadResponse = check AssistantClient->/threads.post(createThreadReq, headers);
if (threadResponse is openai_assistants:ThreadObject){
io:println("Thread ID: ", threadResponse.id);
} else{
io:println("Error creating thread: ", threadResponse);
}
}
```

### Step 4: 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 — we smartly truncate any context that does not fit into the model's context window.

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

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

// Create a message in the thread
var messageResponse = check AssistantClient->/threads/[threadId]/messages.post(createMsgReq, headers);
if (messageResponse is openai_assistants:MessageObject){
io:println("Created Message: ", messageResponse);
} else {
io:println("Error creating Message: ", messageResponse);
}
}
```


### Step 5: 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";

openai_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
var resp = AssistantClient->/threads/[threadId]/runs.post(runReq, headers);
if (resp is openai_assistants:RunObject) {
io:println("Created Run: ", resp);
} else {
io:println("Error creating run: ", resp);
}
}
```
Once the Run completes, you can list the Messages added to the Thread by the Assistant.
```ballerina
public function main() returns error?{
SanduniU marked this conversation as resolved.
Show resolved Hide resolved
string threadId = "your_thread_id";

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

// List messages in the thread
var res = AssistantClient->/threads/[threadId]/messages.get(headers);
if (res is openai_assistants:ListMessagesResponse) {
io:println("Messages of Thread: ", res);
} else {
io:println("Error retrieving messages: ", res);
}
}
```

## Examples

Expand Down
Loading