|
| 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 | +``` |
0 commit comments