-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Do not attempt to call functions without arguments * Creates or updates an Assistant based on YAML config * Use https://github.com/Him188/yamlkt to hand parse in kmp * removed println * Apply spotless formatting * Update core/src/commonMain/kotlin/com/xebia/functional/xef/llm/assistants/Assistant.kt Co-authored-by: David Vega Lichacz <7826728+realdavidvega@users.noreply.github.com> * Update core/src/commonMain/kotlin/com/xebia/functional/xef/llm/assistants/Assistant.kt Co-authored-by: David Vega Lichacz <7826728+realdavidvega@users.noreply.github.com> * Apply spotless formatting * fix fqn * example * YAML is able to load tools from the tool config * use simple name for kmp support * review comments --------- Co-authored-by: raulraja <raulraja@users.noreply.github.com> Co-authored-by: David Vega <7826728+realdavidvega@users.noreply.github.com>
- Loading branch information
1 parent
4d8c0a4
commit 34ad9d2
Showing
5 changed files
with
222 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
core/src/commonMain/kotlin/com/xebia/functional/xef/llm/assistants/AssistantRequest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package com.xebia.functional.xef.llm.assistants | ||
|
||
import kotlinx.serialization.Required | ||
import kotlinx.serialization.SerialName | ||
import kotlinx.serialization.Serializable | ||
|
||
@Serializable | ||
data class AssistantRequest( | ||
@SerialName(value = "assistant_id") val assistantId: String? = null, | ||
@SerialName(value = "model") @Required val model: String, | ||
|
||
/* The name of the assistant. The maximum length is 256 characters. */ | ||
@SerialName(value = "name") val name: String? = null, | ||
|
||
/* The description of the assistant. The maximum length is 512 characters. */ | ||
@SerialName(value = "description") val description: String? = null, | ||
|
||
/* The system instructions that the assistant uses. The maximum length is 32768 characters. */ | ||
@SerialName(value = "instructions") val instructions: String? = null, | ||
|
||
/* A list of tool enabled on the assistant. There can be a maximum of 128 tools per assistant. Tools can be of types `code_interpreter`, `retrieval`, or `function`. */ | ||
@SerialName(value = "tools") val tools: List<AssistantTool>? = arrayListOf(), | ||
|
||
/* A list of [file](/docs/api-reference/files) IDs attached to this assistant. There can be a maximum of 20 files attached to the assistant. Files are ordered by their creation date in ascending order. */ | ||
@SerialName(value = "file_ids") val fileIds: List<String>? = arrayListOf(), | ||
|
||
/* Set of 16 key-value pairs that can be attached to an object. This can be useful for storing additional information about the object in a structured format. Keys can be a maximum of 64 characters long and values can be a maxium of 512 characters long. */ | ||
@SerialName(value = "metadata") val metadata: Map<String, String>? = null | ||
) | ||
|
||
@Serializable | ||
sealed class AssistantTool { | ||
@Serializable @SerialName(value = "code_interpreter") object CodeInterpreter : AssistantTool() | ||
|
||
@Serializable @SerialName(value = "retrieval") object Retrieval : AssistantTool() | ||
|
||
@Serializable | ||
@SerialName(value = "function") | ||
data class Function(val name: String, val description: String, val parameters: String) : | ||
AssistantTool() | ||
} |
58 changes: 58 additions & 0 deletions
58
examples/src/main/kotlin/com/xebia/functional/xef/assistants/LoadFromYamlConfig.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package com.xebia.functional.xef.assistants | ||
|
||
import com.xebia.functional.xef.llm.assistants.Assistant | ||
import com.xebia.functional.xef.llm.assistants.AssistantThread | ||
import com.xebia.functional.xef.llm.assistants.MessageWithFiles | ||
import com.xebia.functional.xef.llm.assistants.Tool | ||
|
||
suspend fun main() { | ||
// val filesApi = fromEnvironment(::FilesApi) | ||
// // should only be created once and then referenced by id | ||
// val file = | ||
// filesApi | ||
// .createFile( | ||
// UploadFile("test.txt") { append("Hello World!") }, | ||
// FilesApi.PurposeCreateFile.assistants | ||
// ) | ||
// .body() | ||
val fileId = "file-q77cZu6e6sC2TsYbUs8UX5Dj" | ||
// remove assistant id to create a new one | ||
// language=yaml | ||
val yamlConfig = | ||
""" | ||
assistant_id: "asst_ImCKap37lLHBqV1awa0kzjZ3" | ||
model: "gpt-4-1106-preview" | ||
name: "My Custom Test Assistant" | ||
description: "A versatile AI assistant capable of conversational and informational tasks." | ||
instructions: "This assistant is designed to provide informative and engaging conversations, answer queries, and execute code when necessary." | ||
tools: | ||
- type: "code_interpreter" | ||
- type: "retrieval" | ||
- type: "function" | ||
name: "SumTool" | ||
file_ids: | ||
- "$fileId" | ||
metadata: | ||
version: "1.0" | ||
created_by: "OpenAI" | ||
use_case: "Customer support" | ||
language: "English" | ||
additional_info: "This assistant is continuously updated with the latest information." | ||
""" | ||
.trimIndent() | ||
val tools = listOf(Tool.toolOf(SumTool())) | ||
val assistant = Assistant.fromConfig(request = yamlConfig, toolsConfig = tools) | ||
val assistantInfo = assistant.get() | ||
println("assistant: $assistantInfo") | ||
val thread = AssistantThread() | ||
thread.createMessage(MessageWithFiles("What does this file say?", listOf(fileId))) | ||
val stream = thread.run(assistant) | ||
stream.collect { | ||
when (it) { | ||
is AssistantThread.RunDelta.ReceivedMessage -> | ||
println("received message: ${it.message.content.firstOrNull()?.text}") | ||
is AssistantThread.RunDelta.Run -> println("run: ${it.message.status.value}") | ||
is AssistantThread.RunDelta.Step -> println("step: ${it.runStep.type.value}") | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters