Skip to content

Commit 8180bfa

Browse files
committed
chore: update guides and docs
1 parent 0ce9d79 commit 8180bfa

File tree

35 files changed

+1061
-382
lines changed

35 files changed

+1061
-382
lines changed

README.md

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,15 @@ Use your `OpenAI` instance to make API requests. [Learn more](guides/GettingStar
8282
- [Moderations](guides/GettingStarted.md#moderations)
8383
- [Audio](guides/GettingStarted.md#audio)
8484

85-
#### Legacy
86-
- [Completions](guides/GettingStarted.md#completions)
85+
#### Beta
86+
87+
- [Assistants](guides/GettingStarted.md#assistants)
88+
- [Threads](guides/GettingStarted.md#threads)
89+
- [Messages](guides/GettingStarted.md#messages)
90+
- [Runs](guides/GettingStarted.md#runs)
8791

8892
#### Deprecated
93+
- [Completions](guides/GettingStarted.md#completions)
8994
- [Fine-tunes](guides/GettingStarted.md#fine-tunes)
9095
- [Edits](guides/GettingStarted.md#edits)
9196

@@ -96,8 +101,9 @@ Use your `OpenAI` instance to make API requests. [Learn more](guides/GettingStar
96101
Get started and understand more about how to use OpenAI API client for Kotlin with these guides:
97102

98103
- [Getting Started](guides/GettingStarted.md)
99-
- [Chat with Function Call](guides/ChatFunctionCall.md)
104+
- [Chat & Function Call](guides/ChatToolCalls.md)
100105
- [FileSource Guide](guides/FileSource.md)
106+
- [Assistants](guides/Assistants.md)
101107

102108
## ℹ️ Sample apps
103109

guides/Assistants.md

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
# Assistants
2+
3+
This guide provides step-by-step instructions on how to integrate OpenAI's Assistants API in a Kotlin application.
4+
5+
## Prerequisites
6+
7+
- OpenAI API Client for Kotlin installed.
8+
- An API key from OpenAI.
9+
10+
## Steps to Follow
11+
12+
### Step 1: Setting Up the Environment
13+
14+
Ensure that you have the necessary OpenAI SDK and dependencies in your Kotlin project.
15+
Set the `OPENAI_API_KEY` environment variable with your API key.
16+
17+
```kotlin
18+
val token = System.getenv("OPENAI_API_KEY")
19+
val openAI = OpenAI(token)
20+
```
21+
22+
### Step 2: Create an Assistant
23+
24+
```kotlin
25+
val assistant = openAI.assistant(
26+
request = AssistantRequest(
27+
name = "Math Tutor",
28+
instructions = "You are a personal math tutor. Write and run code to answer math questions.",
29+
tools = listOf(AssistantTool.CodeInterpreter),
30+
model = ModelId("gpt-4-1106-preview")
31+
)
32+
)
33+
```
34+
35+
This code snippet creates an assistant named "Math Tutor" with instructions and tools specified.
36+
37+
### Step 3: Create a Thread
38+
39+
```kotlin
40+
val thread = openAI.thread()
41+
```
42+
43+
Create a thread to start a conversation. Each user should have their own thread.
44+
45+
### Step 4: Add a Message to the Thread
46+
47+
```kotlin
48+
openAI.message(
49+
threadId = thread.id,
50+
request = MessageRequest(
51+
role = Role.User,
52+
content = "I need to solve the equation `3x + 11 = 14`. Can you help me?"
53+
)
54+
)
55+
```
56+
57+
Add a message to the thread. In this example, a math problem is posed.
58+
59+
### Step 5: Run the Assistant
60+
61+
```kotlin
62+
val run = openAI.createRun(
63+
thread.id,
64+
request = RunRequest(
65+
assistantId = assistant.id,
66+
instructions = "Please address the user as Jane Doe. The user has a premium account.",
67+
)
68+
)
69+
```
70+
71+
Create a run to process the user's message and generate a response.
72+
73+
74+
### Step 6: Check Run Status and Display Response
75+
76+
```kotlin
77+
do {
78+
delay(1500)
79+
val retrievedRun = openAI.getRun(threadId = thread.id, runId = run.id)
80+
} while (retrievedRun.status != Status.Completed)
81+
82+
val assistantMessages = openAI.messages(thread.id)
83+
println("\nThe assistant's response:")
84+
for (message in assistantMessages) {
85+
val textContent = message.content.first() as? MessageContent.Text ?: error("Expected MessageContent.Text")
86+
println(textContent.text.value)
87+
}
88+
```
89+
90+
Check the status of the run until it is completed, then display the assistant's response.
91+
92+
## Conclusion
93+
94+
This guide covers the basics of setting up and using the OpenAI Assistants API in a Kotlin project.
95+
Remember to explore additional features and capabilities of the API for more advanced use cases.
96+
97+
### Complete Example
98+
99+
Below is a complete Kotlin example following the guide:
100+
101+
```kotlin
102+
suspend fun main() {
103+
104+
// 1. Setup client
105+
val token = System.getenv("OPENAI_API_KEY")
106+
val openAI = OpenAI(token)
107+
108+
// 2. Create an Assistant
109+
val assistant = openAI.assistant(
110+
request = AssistantRequest(
111+
name = "Math Tutor",
112+
instructions = "You are a personal math tutor. Write and run code to answer math questions.",
113+
tools = listOf(AssistantTool.CodeInterpreter),
114+
model = ModelId("gpt-4-1106-preview")
115+
)
116+
)
117+
118+
// 3. Create a thread
119+
val thread = openAI.thread()
120+
121+
// 4. Add a message to the thread
122+
openAI.message(
123+
threadId = thread.id,
124+
request = MessageRequest(
125+
role = Role.User,
126+
content = "I need to solve the equation `3x + 11 = 14`. Can you help me?"
127+
)
128+
)
129+
val messages = openAI.messages(thread.id)
130+
println("List of messages in the thread:")
131+
for (message in messages) {
132+
val textContent = message.content.first() as? MessageContent.Text ?: error("Expected MessageContent.Text")
133+
println(textContent.text.value)
134+
}
135+
136+
// 5. Run the assistant
137+
val run = openAI.createRun(
138+
thread.id,
139+
request = RunRequest(
140+
assistantId = assistant.id,
141+
instructions = "Please address the user as Jane Doe. The user has a premium account.",
142+
)
143+
)
144+
145+
// 6. Check the run status
146+
do {
147+
delay(1500)
148+
val retrievedRun = openAI.getRun(threadId = thread.id, runId = run.id)
149+
} while (retrievedRun.status != Status.Completed)
150+
151+
// 6. Display the assistant's response
152+
val assistantMessages = openAI.messages(thread.id)
153+
println("\nThe assistant's response:")
154+
for (message in assistantMessages) {
155+
val textContent = message.content.first() as? MessageContent.Text ?: error("Expected MessageContent.Text")
156+
println(textContent.text.value)
157+
}
158+
}
159+
```

guides/ChatFunctionCall.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
# Chat with Function Call
1+
# Chat with Function Call (deprecated)
2+
3+
> Function calls are deprecated. Please use [Chat & Tool Calls](ChatToolCalls.md) instead.
24
35
This guide is designed to demonstrate the interaction with OpenAI API using the Kotlin language to execute a chat completion request with function calls.
46

57
## Prerequisites
68

7-
- The OpenAI Kotlin SDK installed.
9+
- OpenAI API Client for Kotlin installed.
810
- An API key from OpenAI.
911

1012
## Overview
@@ -27,7 +29,7 @@ val openAI = OpenAI(token)
2729
Specify the model to use for the chat request.
2830

2931
```kotlin
30-
val modelId = ModelId("gpt-3.5-turbo")
32+
val modelId = ModelId("gpt-3.5-turbo-0613")
3133
```
3234

3335
### Defining the Function
@@ -166,7 +168,7 @@ suspend fun main() {
166168
val token = System.getenv("OPENAI_API_KEY")
167169
val openAI = OpenAI(token)
168170

169-
val modelId = ModelId("gpt-3.5-turbo")
171+
val modelId = ModelId("gpt-3.5-turbo-0613")
170172
val chatMessages = mutableListOf(
171173
ChatMessage(
172174
role = ChatRole.User,

0 commit comments

Comments
 (0)