Created
March 20, 2025 17:17
-
-
Save antonarhipov/48aacbf8d7d329d7df2767b54f6d0575 to your computer and use it in GitHub Desktop.
gettings started with Spring AI in Kotlin Notebooks
This file contains hidden or 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
{ | |
"cells" : [ { | |
"metadata" : { }, | |
"cell_type" : "markdown", | |
"source" : "# Getting started with Spring AI", | |
"id" : "ace3afb974e39532" | |
}, { | |
"metadata" : { | |
"ExecuteTime" : { | |
"end_time" : "2025-03-20T17:17:04.833165Z", | |
"start_time" : "2025-03-20T17:17:02.681758Z" | |
} | |
}, | |
"cell_type" : "code", | |
"source" : [ "USE {\n", " dependencies {\n", " val springAiVersion = \"1.0.0-M6\"\n", " implementation(\"org.springframework.ai:spring-ai-openai:$springAiVersion\")\n", " implementation(\"org.springframework.ai:spring-ai-openai-spring-boot-starter:$springAiVersion\")\n", " implementation(\"com.fasterxml.jackson.module:jackson-module-kotlin:2.18.2\")\n", " }\n", "}" ], | |
"id" : "af9ddb7d02f5de00", | |
"outputs" : [ ], | |
"execution_count" : 17 | |
}, { | |
"metadata" : { }, | |
"cell_type" : "markdown", | |
"source" : "### Keep your API keys safe", | |
"id" : "a1c8fd27e9c7b030" | |
}, { | |
"metadata" : { | |
"ExecuteTime" : { | |
"end_time" : "2025-03-20T17:13:52.930874Z", | |
"start_time" : "2025-03-20T17:13:52.744145Z" | |
} | |
}, | |
"cell_type" : "code", | |
"source" : "val apiKey = System.getenv(\"OPENAI_API_KEY\") ?: \"YOUR_OPENAI_API_KEY\"", | |
"id" : "8561216601457894", | |
"outputs" : [ ], | |
"execution_count" : 6 | |
}, { | |
"metadata" : { }, | |
"cell_type" : "markdown", | |
"source" : "### Setup the chat model", | |
"id" : "e6d7ae57cd0d3891" | |
}, { | |
"metadata" : { | |
"ExecuteTime" : { | |
"end_time" : "2025-03-20T17:13:53.172257Z", | |
"start_time" : "2025-03-20T17:13:52.938827Z" | |
} | |
}, | |
"cell_type" : "code", | |
"source" : [ "import org.springframework.ai.openai.OpenAiChatModel\n", "import org.springframework.ai.openai.OpenAiChatOptions\n", "import org.springframework.ai.openai.api.OpenAiApi\n", "\n", "val openAiApi = OpenAiApi.builder().apiKey(apiKey).build()\n", "val openAiChatOptions = OpenAiChatOptions.builder()\n", " .model(OpenAiApi.ChatModel.GPT_4_O_MINI)\n", " .temperature(0.7)\n", " .build()\n", "val chatModel = OpenAiChatModel.builder().openAiApi(openAiApi).defaultOptions(openAiChatOptions).build()" ], | |
"id" : "881400ec49f7da7e", | |
"outputs" : [ ], | |
"execution_count" : 7 | |
}, { | |
"metadata" : { }, | |
"cell_type" : "markdown", | |
"source" : "### Prompt the model", | |
"id" : "d9a88a13896b1b26" | |
}, { | |
"metadata" : { | |
"ExecuteTime" : { | |
"end_time" : "2025-03-20T17:14:45.054423Z", | |
"start_time" : "2025-03-20T17:14:43.805969Z" | |
} | |
}, | |
"cell_type" : "code", | |
"source" : [ "import org.springframework.ai.chat.client.ChatClient\n", "\n", "data class Joke(val setup: String, val punchline: String)\n", "\n", "val joke = ChatClient.create(chatModel).prompt().user(\"Tell me a dad's joke\").call().entity(Joke::class.java)\n", "\n", "println(\"Setup: ${joke.setup}\\nPunchline: ${joke.punchline}\")" ], | |
"id" : "9fb32ac89fe7cda4", | |
"outputs" : [ { | |
"name" : "stdout", | |
"output_type" : "stream", | |
"text" : [ "Setup: Why can't you give Elsa a balloon?\n", "Punchline: Because she will let it go!\n" ] | |
} ], | |
"execution_count" : 15 | |
} ], | |
"metadata" : { | |
"kernelspec" : { | |
"display_name" : "Kotlin", | |
"language" : "kotlin", | |
"name" : "kotlin" | |
}, | |
"language_info" : { | |
"name" : "kotlin", | |
"version" : "1.9.23", | |
"mimetype" : "text/x-kotlin", | |
"file_extension" : ".kt", | |
"pygments_lexer" : "kotlin", | |
"codemirror_mode" : "text/x-kotlin", | |
"nbconvert_exporter" : "" | |
} | |
}, | |
"nbformat" : 4, | |
"nbformat_minor" : 0 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment