### Generate Completion Response (Streaming) Example Source: https://github.com/tddworks/openai-kotlin/blob/main/ollama-client/api.md Example of a streaming response from the /api/generate endpoint. The initial responses indicate the ongoing generation process, while the final response includes comprehensive statistics like total duration, load duration, and token counts. ```json { "model": "llama2", "created_at": "2023-08-04T08:52:19.385406455-07:00", "response": "The", "done": false } ``` ```json { "model": "llama2", "created_at": "2023-08-04T19:22:45.499127Z", "response": "", "done": true, "context": [1, 2, 3], "total_duration": 10706818083, "load_duration": 6338219291, "prompt_eval_count": 26, "prompt_eval_duration": 130079000, "eval_count": 259, "eval_duration": 4232710000 } ``` -------------------------------- ### Load a Model Source: https://github.com/tddworks/openai-kotlin/blob/main/ollama-client/api.md This example shows how to load a model into memory by sending a request to the Ollama API with an empty prompt. This is useful for pre-loading models before making generation requests. ```shell curl http://localhost:11434/api/generate -d '{ "model": "llama2" }' ``` -------------------------------- ### Generate Text Completion (Reproducible Output) Source: https://github.com/tddworks/openai-kotlin/blob/main/ollama-client/api.md This example shows how to generate a text completion with reproducible outputs by setting the 'seed' and 'temperature' options. This is useful for testing and debugging. ```shell curl http://localhost:11434/api/generate -d '{ "model": "mistral", "prompt": "Why is the sky blue?", "options": { "seed": 123, "temperature": 0 } }' ``` -------------------------------- ### Push a Model Source: https://github.com/tddworks/openai-kotlin/blob/main/ollama-client/api.md Uploads a model to a model library. Requires prior registration and public key setup. ```APIDOC ## POST /api/push ### Description Upload a model to a model library. Requires registering for ollama.ai and adding a public key first. ### Method POST ### Endpoint /api/push ### Parameters #### Request Body - **name** (string) - Required - The name of the model to push in the format `/:`. - **insecure** (boolean) - Optional - Allow insecure connections to the library. Only use this if you are pushing to your library during development. - **stream** (boolean) - Optional - If `false`, the response will be returned as a single response object, rather than a stream of objects. Defaults to `true`. ### Request Example ```json { "name": "mattw/pygmalion:latest" } ``` ### Response #### Success Response (200) Returns a 200 OK status upon successful upload. The response format depends on the `stream` parameter, similar to the `/api/pull` endpoint. ``` -------------------------------- ### Generate Text Completion (With Advanced Options) Source: https://github.com/tddworks/openai-kotlin/blob/main/ollama-client/api.md This example demonstrates making a text generation request with a comprehensive set of advanced options, including parameters like num_keep, seed, top_k, temperature, and more, to fine-tune model behavior. ```shell curl http://localhost:11434/api/generate -d '{ "model": "llama2", "prompt": "Why is the sky blue?", "stream": false, "options": { "num_keep": 5, "seed": 42, "num_predict": 100, "top_k": 20, "top_p": 0.9, "tfs_z": 0.5, "typical_p": 0.7, "repeat_last_n": 33, "temperature": 0.8, "repeat_penalty": 1.2, "presence_penalty": 1.5, "frequency_penalty": 1.0, "mirostat": 1, "mirostat_tau": 0.8, "mirostat_eta": 0.6, "penalize_newline": true, "stop": ["\n", "user:"], "numa": false, "num_ctx": 1024, "num_batch": 2, "num_gqa": 1, "num_gpu": 1, "main_gpu": 0, "low_vram": false, "f16_kv": true, "vocab_only": false, "use_mmap": true, "use_mlock": false, "rope_frequency_base": 1.1, "rope_frequency_scale": 0.8, "num_thread": 8 } }' ``` -------------------------------- ### Generate Embeddings Request Example (Shell) Source: https://github.com/tddworks/openai-kotlin/blob/main/ollama-client/api.md Shows how to make a POST request to the /api/embeddings endpoint using cURL. It includes the necessary parameters 'model' and 'prompt', and demonstrates how to send them as form data. ```shell curl http://localhost:11434/api/embeddings -d '{ "model": "all-minilm", "prompt": "Here is an article about llamas..." }' ``` -------------------------------- ### Example JSON Response from OpenAI API Source: https://github.com/tddworks/openai-kotlin/blob/main/ollama-client/api.md This is an example of a JSON response received from the OpenAI API when the 'format' parameter is set to 'json'. The 'response' field contains a string which itself is a JSON object detailing the model's answer. The entire response object includes metadata like creation time, generation status, and performance metrics. ```json { "model": "llama2", "created_at": "2023-11-09T21:07:55.186497Z", "response": "{\n\"morning\": {\n\"color\": \"blue\"\n},\n\"noon\": {\n\"color\": \"blue-gray\"\n},\n\"afternoon\": {\n\"color\": \"warm gray\"\n},\n\"evening\": {\n\"color\": \"orange\"\n}\n}", "done": true, "context": [ 1, 2, 3 ], "total_duration": 4648158584, "load_duration": 4071084, "prompt_eval_count": 36, "prompt_eval_duration": 439038000, "eval_count": 180, "eval_duration": 4196918000 } ``` -------------------------------- ### Generate Images with OpenAI API Client Source: https://github.com/tddworks/openai-kotlin/blob/main/README.md Example of generating images using the OpenAI API client. Requires specifying the prompt, size, quality, and number of images to create. The result is a list of image URLs. ```kotlin val images = openAI.images( ImageCreate( prompt = "A beautiful sunset over mountains", size = Size.SIZE_1024x1024, quality = Quality.HD, n = 1 ) ) ``` -------------------------------- ### Interact with Anthropic Claude API Source: https://github.com/tddworks/openai-kotlin/blob/main/README.md Example of initializing and using the Anthropic Claude API client. Requires an API key and allows sending messages to the Claude model, specifying the model and max tokens for the response. ```kotlin val claude = initAnthropic( AnthropicConfig(apiKey = { "your-anthropic-key" }) ) val message = claude.messages( CreateMessageRequest( messages = listOf( Message( role = Role.USER, content = listOf(ContentMessage.TextContent("Explain quantum computing")) ) ), model = AnthropicModel.CLAUDE_3_SONNET, maxTokens = 1000 ) ) ``` -------------------------------- ### Generate Completion Response (No Streaming) Example Source: https://github.com/tddworks/openai-kotlin/blob/main/ollama-client/api.md An example of a non-streaming response from the /api/generate endpoint. The entire completion is returned in a single JSON object, including the generated `response` and performance metrics. ```json { "model": "llama2", "created_at": "2023-08-04T19:22:45.499127Z", "response": "The sky is blue because it is the color of the sky.", "done": true, "context": [1, 2, 3], "total_duration": 5043500667, "load_duration": 5025959, "prompt_eval_count": 26, "prompt_eval_duration": 325953000, "eval_count": 290, "eval_duration": 4709213000 } ``` -------------------------------- ### Ollama Show Model Response Example Source: https://github.com/tddworks/openai-kotlin/blob/main/ollama-client/api.md A JSON object detailing information about a specific model requested via the `/api/show` endpoint. It includes the model's modelfile content, parameters, template, and technical details. ```json { "modelfile": "# Modelfile generated by \"ollama show\"\n# To build a new Modelfile based on this one, replace the FROM line with:\n# FROM llava:latest\n\nFROM /Users/matt/.ollama/models/blobs/sha256:200765e1283640ffbd013184bf496e261032fa75b99498a9613be4e94d63ad52\nTEMPLATE \"\"\"{{ .System }}\nUSER: {{ .Prompt }}\nASSSISTANT: \"\"\nPARAMETER num_ctx 4096\nPARAMETER stop \"\u003c/s\u003e\"\nPARAMETER stop \"USER:\"\nPARAMETER stop \"ASSSISTANT:\"", "parameters": "num_ctx 4096\nstop \u003c/s\u003e\nstop USER:\nstop ASSSISTANT:", "template": "{{ .System }}\nUSER: {{ .Prompt }}\nASSSISTANT: ", "details": { "format": "gguf", "family": "llama", "families": ["llama", "clip"], "parameter_size": "7B", "quantization_level": "Q4_0" } } ``` -------------------------------- ### OpenAI API Response Streaming Examples (JSON) Source: https://github.com/tddworks/openai-kotlin/blob/main/ollama-client/api.md Demonstrates the JSON response formats when the 'stream' parameter is not specified or set to true. Includes initial status updates, upload progress, and final success messages. This format is useful for real-time monitoring of operations. ```json { "status": "retrieving manifest" } ``` ```json { "status": "starting upload", "digest": "sha256:bc07c81de745696fdf5afca05e065818a8149fb0c77266fb584d9b2cba3711ab", "total": 1928429856 } ``` ```json { "status": "starting upload", "digest": "sha256:bc07c81de745696fdf5afca05e065818a8149fb0c77266fb584d9b2cba3711ab", "total": 1928429856 } ``` ```json {"status":"pushing manifest"} {"status":"success"} ``` -------------------------------- ### Ollama Models Response Example Source: https://github.com/tddworks/openai-kotlin/blob/main/ollama-client/api.md A JSON object representing a list of available models in the Ollama library. Each model entry includes its name, modification timestamp, size, digest, and detailed format and parameter information. ```json { "models": [ { "name": "codellama:13b", "modified_at": "2023-11-04T14:56:49.277302595-07:00", "size": 7365960935, "digest": "9f438cb9cd581fc025612d27f7c1a6669ff83a8bb0ed86c94fcf4c5440555697", "details": { "format": "gguf", "family": "llama", "families": null, "parameter_size": "13B", "quantization_level": "Q4_0" } }, { "name": "llama2:latest", "modified_at": "2023-12-07T09:32:18.757212583-08:00", "size": 3825819519, "digest": "fe938a131f40e6f6d40083c9f0f430a515233eb2edaa6d72eb85c50d64f2300e", "details": { "format": "gguf", "family": "llama", "families": null, "parameter_size": "7B", "quantization_level": "Q4_0" } } ] } ``` -------------------------------- ### Generate Text Completion (JSON Response) Source: https://github.com/tddworks/openai-kotlin/blob/main/ollama-client/api.md This example demonstrates how to generate a text completion using the Ollama API with a specified model and prompt. It expects a JSON response and includes options for reproducibility with seed and temperature. ```shell curl http://localhost:11434/api/generate -d '{ "model": "mistral", "prompt": "[INST] why is the sky blue? [/INST]", "raw": true, "stream": false }' ``` -------------------------------- ### Generate Chat Completion with Retrofit in Kotlin Source: https://github.com/tddworks/openai-kotlin/blob/main/ollama-client/ollama-client-core/src/commonMain/kotlin/com/tddworks/ollama/implement.md Example of using the `ChatApi` interface with Retrofit to send a chat request and receive a response. It demonstrates creating the Retrofit service, constructing the `ChatRequest`, and calling the `generateChat` method. ```kotlin val chatApi = retrofit.create(ChatApi::class.java) val request = ChatRequest( model = "llama2", messages = listOf( Message( role = "user", content = "why is the sky blue?" ) ) ) val response = chatApi.generateChat(request) ``` -------------------------------- ### Configure OpenAI Client with Custom Base URL Source: https://github.com/tddworks/openai-kotlin/blob/main/README.md Example of configuring the OpenAI client with a custom base URL and API key, potentially overriding default settings. Environment variables can be used for sensitive information like API keys. ```kotlin val openAI = initOpenAI( OpenAIConfig( baseUrl = { "https://api.openai.com/v1" }, apiKey = { System.getenv("OPENAI_API_KEY") }, organization = { "your-org-id" } // optional ) ) ``` -------------------------------- ### JSON Streaming Response Example Source: https://github.com/tddworks/openai-kotlin/blob/main/ollama-client/api.md Demonstrates the structure of a streaming JSON response from the OpenAI API. This is useful for receiving partial responses as they are generated, providing a more interactive experience. The response includes model information, timestamps, message content, and a 'done' flag indicating if the stream is complete. ```json { "model": "llama2", "created_at": "2023-08-04T08:52:19.385406455-07:00", "message": { "role": "assistant", "content": "The", "images": null }, "done": false } ``` ```json { "model": "llama2", "created_at": "2023-08-04T19:22:45.499127Z", "done": true, "total_duration": 4883583458, "load_duration": 1334875, "prompt_eval_count": 26, "prompt_eval_duration": 342546000, "eval_count": 282, "eval_duration": 4535599000 } ``` -------------------------------- ### Build and Run Unit Tests Source: https://github.com/tddworks/openai-kotlin/blob/main/README.md Command to execute unit tests for the project using Gradle. This is a standard way to verify the correctness of individual code components. ```bash ./gradlew test ``` -------------------------------- ### Build Project with Gradle Source: https://github.com/tddworks/openai-kotlin/blob/main/CLAUDE.md Commands to build the entire project or clean and then build. Ensures all modules are compiled and ready for use. ```bash ./gradlew build ./gradlew clean build ./gradlew assemble ``` -------------------------------- ### Gradle Build and Test Commands Source: https://context7.com/tddworks/openai-kotlin/llms.txt Provides essential Gradle commands for building, testing, and generating reports for the multiplatform Kotlin library. Includes commands for building all modules, running tests across different platforms, checking test coverage, cleaning the project, and verifying tasks. ```bash # Build all modules ./gradlew build # Run tests for all platforms ./gradlew test # Run platform-specific tests ./gradlew jvmTest ./gradlew macosArm64Test ./gradlew iosSimulatorArm64Test # Generate test coverage reports (minimum 86% required) ./gradlew koverHtmlReport ./gradlew koverVerify # Run tests for specific modules ./gradlew :openai-client:openai-client-core:test ./gradlew :anthropic-client:anthropic-client-core:test ./gradlew :ollama-client:ollama-client-core:test ./gradlew :gemini-client:gemini-client-core:test ./gradlew :openai-gateway:openai-gateway-core:test # Clean and rebuild ./gradlew clean build # Assemble without running tests ./gradlew assemble # Run all verification tasks ./gradlew check ``` -------------------------------- ### Run Tests with Gradle Source: https://github.com/tddworks/openai-kotlin/blob/main/CLAUDE.md Commands to execute tests across different platforms and configurations. Includes options for all tests, specific platforms, or just JVM tests. ```bash ./gradlew test ./gradlew jvmTest ./gradlew macosArm64Test ./gradlew iosSimulatorArm64Test ./gradlew allTests ./gradlew check ``` -------------------------------- ### Initialize Basic OpenAI Client in Kotlin Source: https://github.com/tddworks/openai-kotlin/blob/main/README.md Demonstrates how to initialize and use a basic OpenAI client in Kotlin. It requires an API key for authentication and allows making chat completion requests. ```kotlin import com.tddworks.openai.api.OpenAIConfig import com.tddworks.openai.api.chat.api.* import com.tddworks.openai.di.initOpenAI val openAI = initOpenAI( OpenAIConfig( apiKey = { "your-api-key" } ) ) // Chat completion val response = openAI.chatCompletions( ChatCompletionRequest( messages = listOf(ChatMessage.UserMessage("Hello, world!")), model = Model.GPT_4O, maxTokens = 1000 ) ) // Streaming chat completion openAI.streamChatCompletions( ChatCompletionRequest( messages = listOf(ChatMessage.UserMessage("Tell me a story")), model = Model.GPT_4O ) ).collect { chunk -> print(chunk.choices?.firstOrNull()?.delta?.content ?: "") } ``` -------------------------------- ### Run Integration Tests Source: https://github.com/tddworks/openai-kotlin/blob/main/README.md Command to execute integration tests for the project using Gradle. These tests typically verify the interaction between different modules or with external services. ```bash ./gradlew integrationTest ``` -------------------------------- ### Connect to Local Ollama LLM Server Source: https://github.com/tddworks/openai-kotlin/blob/main/README.md Shows how to initialize and use the Ollama client to interact with a local LLM server. Configuration includes the base URL and port of the Ollama instance. ```kotlin val ollama = initOllama( OllamaConfig(baseUrl = "localhost", port = 11434) ) val response = ollama.chat( OllamaChatRequest( model = OllamaModel.LLAMA2.value, messages = listOf( OllamaChatMessage( role = "user", content = "What is the capital of France?" ) ) ) ) ``` -------------------------------- ### Chat Completion (Streaming) Source: https://github.com/tddworks/openai-kotlin/blob/main/ollama-client/api.md This example demonstrates how to generate a chat completion using the Ollama API. It sends a user message within a conversation and expects a streaming response, allowing for real-time message updates. ```shell curl http://localhost:11434/api/chat -d '{ "model": "llama2", "messages": [ { "role": "user", "content": "why is the sky blue?" } ] }' ``` -------------------------------- ### OpenAI Chat Completions using Kotlin Source: https://context7.com/tddworks/openai-kotlin/llms.txt Demonstrates how to create and stream chat completions using OpenAI's GPT models via the Kotlin Multiplatform library. It initializes the OpenAI client with configuration, sends chat completion requests, and processes streaming responses. ```kotlin import com.tddworks.openai.api.OpenAI import com.tddworks.openai.api.OpenAIConfig import com.tddworks.openai.api.chat.api.ChatCompletionRequest import com.tddworks.openai.api.chat.api.ChatMessage import com.tddworks.openai.api.chat.api.OpenAIModel import com.tddworks.openai.di.initOpenAI // Initialize OpenAI client val openAI = initOpenAI( OpenAIConfig( baseUrl = { OpenAI.BASE_URL }, apiKey = { System.getenv("OPENAI_API_KEY") ?: "your-api-key" } ) ) // Create a simple chat completion val response = openAI.chatCompletions( ChatCompletionRequest( messages = listOf(ChatMessage.UserMessage("hello")), maxTokens = 1024, model = OpenAIModel.GPT_3_5_TURBO ) ) println(response.choices[0].message.content) // Stream chat completion for real-time responses openAI.streamChatCompletions( ChatCompletionRequest( messages = listOf( ChatMessage.SystemMessage("You are a helpful assistant"), ChatMessage.UserMessage("Tell me a joke") ), maxTokens = 1024, model = OpenAIModel.GPT_4O, temperature = 0.7 ) ).collect { chunk -> println(chunk.choices?.get(0)?.delta?.content ?: "") } ``` -------------------------------- ### Initialize Multi-Provider AI Gateway in Kotlin Source: https://github.com/tddworks/openai-kotlin/blob/main/README.md Shows how to set up a unified gateway for multiple AI providers (OpenAI, Anthropic, Ollama) in Kotlin. This allows switching between providers using a single interface. ```kotlin import com.tddworks.openai.gateway.di.initOpenAIGateway import com.tddworks.openai.gateway.api.* val gateway = initOpenAIGateway( defaultProvider = DefaultOpenAIProviderConfig( apiKey = { "openai-api-key" } ), anthropicProvider = AnthropicOpenAIProviderConfig( apiKey = { "anthropic-api-key" } ), ollamaProvider = OllamaOpenAIProviderConfig( baseUrl = { "localhost" }, port = { 11434 } ) ) // Use any provider with the same interface val response = gateway.chatCompletions( ChatCompletionRequest( messages = listOf(ChatMessage.UserMessage("Compare AI models")), model = Model("claude-3-sonnet") // or "llama2", "gpt-4", etc. ) ) ``` -------------------------------- ### Generate Code Coverage Report Source: https://github.com/tddworks/openai-kotlin/blob/main/README.md Command to generate an HTML report for code coverage using Gradle and the Kover plugin. The report is located in the build/reports/kover/html directory. ```bash ./gradlew koverHtmlReport open build/reports/kover/html/index.html ``` -------------------------------- ### Generate Embeddings Response Example (JSON) Source: https://github.com/tddworks/openai-kotlin/blob/main/ollama-client/api.md Presents the JSON structure of a successful response from the /api/embeddings endpoint. It contains a single key 'embedding' whose value is an array of numbers representing the generated embedding vector. ```json { "embedding": [ 0.5670403838157654, 0.009260174818336964, 0.23178744316101074, -0.2916173040866852, -0.8924556970596313, 0.8785552978515625, -0.34576427936553955, 0.5742510557174683, -0.04222835972905159, -0.137906014919281 ] } ``` -------------------------------- ### Generate Test Coverage Reports with Gradle Source: https://github.com/tddworks/openai-kotlin/blob/main/CLAUDE.md Commands to generate code coverage reports in HTML and XML formats. Also includes a command to verify coverage meets a minimum threshold. ```bash ./gradlew koverHtmlReport ./gradlew koverXmlReport ./gradlew koverVerify ``` -------------------------------- ### Initialize and Use Unified Gateway with Multiple LLM Providers in Kotlin Source: https://context7.com/tddworks/openai-kotlin/llms.txt Demonstrates initializing the OpenAIGateway with configurations for OpenAI, Anthropic, Gemini, and Ollama. It shows how to switch between providers using the same `streamChatCompletions` method with different `LLMProvider` types. Dependencies include the openai-gateway API and its internal modules. ```kotlin import com.tddworks.openai.gateway.api.OpenAIGateway import com.tddworks.openai.gateway.api.OpenAIProviderConfig import com.tddworks.openai.gateway.api.LLMProvider import com.tddworks.openai.gateway.api.internal.default import com.tddworks.openai.gateway.api.internal.anthropic import com.tddworks.openai.gateway.api.internal.ollama import com.tddworks.openai.gateway.api.internal.gemini import com.tddworks.openai.gateway.di.initOpenAIGateway // Initialize gateway with multiple providers val gateway = initOpenAIGateway( openAIConfig = OpenAIProviderConfig.default( baseUrl = { "api.openai.com" }, apiKey = { System.getenv("OPENAI_API_KEY") } ), anthropicConfig = OpenAIProviderConfig.anthropic( baseUrl = { "api.anthropic.com" }, apiKey = { System.getenv("ANTHROPIC_API_KEY") }, anthropicVersion = { "2023-06-01" } ), ollamaConfig = OpenAIProviderConfig.ollama(), geminiConfig = OpenAIProviderConfig.gemini( baseUrl = { Gemini.BASE_URL }, apiKey = { System.getenv("GEMINI_API_KEY") } ) ) // Use OpenAI through gateway gateway.streamChatCompletions( ChatCompletionRequest( messages = listOf(ChatMessage.UserMessage("Hello")), maxTokens = 1024, model = OpenAIModel.GPT_3_5_TURBO ), LLMProvider.OPENAI ).collect { chunk -> print(chunk.choices?.get(0)?.delta?.content ?: "") } // Switch to Claude - same interface gateway.streamChatCompletions( ChatCompletionRequest( messages = listOf(ChatMessage.UserMessage("Hello")), maxTokens = 1024, model = OpenAIModel(AnthropicModel.CLAUDE_3_HAIKU.value) ), LLMProvider.ANTHROPIC ).collect { chunk -> print(chunk.choices?.get(0)?.delta?.content ?: "") } // Switch to Gemini - same interface gateway.streamChatCompletions( ChatCompletionRequest( messages = listOf(ChatMessage.UserMessage("Hello")), model = OpenAIModel(GeminiModel.GEMINI_1_5_FLASH.value) ), LLMProvider.GEMINI ).collect { chunk -> print(chunk.choices?.get(0)?.delta?.content ?: "") } // Switch to local Ollama - same interface gateway.streamChatCompletions( ChatCompletionRequest( messages = listOf(ChatMessage.UserMessage("Hello")), maxTokens = 1024, model = OpenAIModel(OllamaModel.LLAMA3.value) ), LLMProvider.OLLAMA ).collect { chunk -> print(chunk.choices?.get(0)?.delta?.content ?: "") } ``` -------------------------------- ### Ollama Pull Status Stream Example Source: https://github.com/tddworks/openai-kotlin/blob/main/ollama-client/api.md A stream of JSON objects representing the status updates during a model pull operation. It includes messages for pulling the manifest, downloading layers with progress, verifying digests, and indicating success. ```json { "status": "pulling manifest" } ``` ```json { "status": "downloading digestname", "digest": "digestname", "total": 2142590208, "completed": 241970 } ``` ```json { "status": "verifying sha256 digest" } ``` ```json { "status": "writing manifest" } ``` ```json { "status": "removing any unused layers" } ``` ```json { "status": "success" } ``` -------------------------------- ### Ollama Pull Success Response Example (Non-Streaming) Source: https://github.com/tddworks/openai-kotlin/blob/main/ollama-client/api.md A single JSON object returned when a model pull operation is completed successfully, specifically when the `stream` parameter is set to `false`. It contains a simple status message indicating success. ```json { "status": "success" } ``` -------------------------------- ### OpenAI Image Generation using Kotlin Source: https://context7.com/tddworks/openai-kotlin/llms.txt Illustrates how to generate images using DALL-E models with the Kotlin Multiplatform library. It shows the creation of image generation requests with parameters like size, quality, and style, and then prints the URLs of the generated images. ```kotlin import com.tddworks.openai.api.images.api.ImageCreate import com.tddworks.openai.api.images.api.Quality import com.tddworks.openai.api.images.api.Size import com.tddworks.openai.api.images.api.Style import com.tddworks.openai.api.chat.api.OpenAIModel // Generate an image with DALL-E 3 val imageRequest = ImageCreate.create( prompt = "A futuristic city with flying cars at sunset", model = OpenAIModel.DALL_E_3, size = Size.LARGE, quality = Quality.HD, style = Style.VIVID ) val images = openAI.generate(imageRequest) images.data.forEach { image -> println("Generated image URL: ${image.url}") println("Revised prompt: ${image.revisedPrompt}") } ``` -------------------------------- ### Parsed JSON Response Structure Source: https://github.com/tddworks/openai-kotlin/blob/main/ollama-client/api.md This JSON structure represents the content of the 'response' field from the previous example, after being parsed. It shows a clear, human-readable breakdown of the sky's color at different times of the day, as generated by the AI model. ```json { "morning": { "color": "blue" }, "noon": { "color": "blue-gray" }, "afternoon": { "color": "warm gray" }, "evening": { "color": "orange" } } ``` -------------------------------- ### Run Specific Module Tests with Gradle Source: https://github.com/tddworks/openai-kotlin/blob/main/CLAUDE.md Commands to run tests for individual modules within the project. Useful for targeted testing during development. ```bash ./gradlew :openai-client:openai-client-core:test ./gradlew :anthropic-client:anthropic-client-core:test ./gradlew :ollama-client:ollama-client-core:test ./gradlew :gemini-client:gemini-client-core:test ./gradlew :openai-gateway:openai-gateway-core:test ``` -------------------------------- ### Final JSON Response with History Example Source: https://github.com/tddworks/openai-kotlin/blob/main/ollama-client/api.md The final JSON object in a streaming response sequence for a chat request that included conversation history. This signifies the end of the response stream. It includes comprehensive details about the model, timings, and evaluation metrics, similar to other final responses. ```json { "model": "llama2", "created_at": "2023-08-04T19:22:45.499127Z", "done": true, "total_duration": 8113331500, "load_duration": 6396458, "prompt_eval_count": 61, "prompt_eval_duration": 398801000, "eval_count": 468, "eval_duration": 7701267000 } ``` -------------------------------- ### JSON Streaming Response with History Example Source: https://github.com/tddworks/openai-kotlin/blob/main/ollama-client/api.md Illustrates the initial part of a streaming JSON response when a chat request includes conversation history. Similar to other streaming responses, it provides incremental updates. The 'done' flag is false, indicating that the full response has not yet been received. ```json { "model": "llama2", "created_at": "2023-08-04T08:52:19.385406455-07:00", "message": { "role": "assistant", "content": "The" }, "done": false } ``` -------------------------------- ### Google Gemini Content Generation and Streaming Source: https://context7.com/tddworks/openai-kotlin/llms.txt Enables content generation using Google's Gemini models, including support for multimodal inputs and system instructions. Allows for both single response generation and streaming of content. Requires a Gemini API key. ```kotlin import com.tddworks.gemini.api.textGeneration.api.Gemini import com.tddworks.gemini.api.textGeneration.api.GeminiConfig import com.tddworks.gemini.api.textGeneration.api.GeminiModel import com.tddworks.gemini.api.textGeneration.api.GenerateContentRequest import com.tddworks.gemini.api.textGeneration.api.Content import com.tddworks.gemini.api.textGeneration.api.Part import com.tddworks.gemini.di.initGemini // Initialize Gemini client val gemini = initGemini( config = GeminiConfig( baseUrl = { Gemini.BASE_URL }, apiKey = { System.getenv("GEMINI_API_KEY") ?: "your-api-key" } ), enableNetworkLogs = false ) // Generate content with system instruction val response = gemini.generateContent( GenerateContentRequest( model = GeminiModel.GEMINI_1_5_FLASH, systemInstruction = Content( parts = listOf(Part.TextPart("You are a creative writing assistant")), role = "system" ), contents = listOf( Content( parts = listOf(Part.TextPart("Write a haiku about programming")), role = "user" ) ) ) ) println(response.candidates?.first()?.content?.parts?.first()?.text) // Stream content generation // Note: The collect function is likely from a reactive streams library like kotlinx.coroutines or Reactor. // For demonstration, we assume its existence. gemini.streamGenerateContent( GenerateContentRequest( model = GeminiModel.GEMINI_2_0_FLASH, contents = listOf( Content( parts = listOf(Part.TextPart("Explain machine learning")), role = "user" ) ) ) ).collect { chunk -> chunk.candidates?.firstOrNull()?.content?.parts?.forEach { part -> print(part.text) } } ``` -------------------------------- ### Show Model Information via API Source: https://github.com/tddworks/openai-kotlin/blob/main/ollama-client/api.md Retrieve detailed information about a specific model using the `/api/show` endpoint. This includes details like the model's file, parameters, template, and format. It requires the model's name as input. ```shell curl http://localhost:11434/api/show -d '{ "name": "llama2" }' ``` -------------------------------- ### Curl Chat Request (No Streaming) Source: https://github.com/tddworks/openai-kotlin/blob/main/ollama-client/api.md Example of making a chat request to the OpenAI API without streaming the response. This is a straightforward curl command that sends a user message and expects a single, complete JSON response. The request body specifies the model and the user's message content. ```shell curl http://localhost:11434/api/chat -d '{ "model": "llama2", "messages": [ { "role": "user", "content": "why is the sky blue?" } ], "stream": false }' ``` -------------------------------- ### Generate JSON Response using cURL Source: https://github.com/tddworks/openai-kotlin/blob/main/ollama-client/api.md This snippet shows how to make a request to the /api/generate endpoint using cURL to get a JSON formatted response. It requires setting the 'format' parameter to 'json' and instructing the model to respond in JSON. The input is a prompt, and the output is a JSON object containing the model's response. ```shell curl http://localhost:11434/api/generate -d '{ "model": "llama2", "prompt": "What color is the sky at different times of the day? Respond using JSON", "format": "json", "stream": false }' ``` -------------------------------- ### Maven Dependency for OpenAI Kotlin Client Source: https://github.com/tddworks/openai-kotlin/blob/main/README.md Shows the Maven XML configuration for adding the OpenAI Kotlin client dependency to a project. ```xml com.tddworks openai-client-jvm 0.2.3 ``` -------------------------------- ### POST /api/generate Source: https://github.com/tddworks/openai-kotlin/blob/main/ollama-client/api.md Generates text completions based on a given prompt and model. Supports streaming, raw mode, and reproducible outputs with seed and temperature options. ```APIDOC ## POST /api/generate ### Description Generates text completions based on a given prompt and model. Supports streaming, raw mode, and reproducible outputs with seed and temperature options. ### Method POST ### Endpoint /api/generate ### Parameters #### Request Body - **model** (string) - Required - The name of the model to use. - **prompt** (string) - Optional - The prompt to generate text from. - **stream** (boolean) - Optional - If false, the response will be returned as a single response object. Defaults to true. - **raw** (boolean) - Optional - Disables templating and provides a full prompt. Does not return context. - **options** (object) - Optional - Advanced model parameters such as `seed`, `temperature`, `top_k`, etc. - **num_keep** (integer) - Optional - How many tokens to keep from the original prompt. - **seed** (integer) - Optional - Seed for reproducible outputs. - **num_predict** (integer) - Optional - The number of tokens to predict. - **top_k** (integer) - Optional - Top-K sampling parameter. - **top_p** (number) - Optional - Top-P (nucleus) sampling parameter. - **tfs_z** (number) - Optional - TFS (temperature scaled) sampling parameter. - **typical_p** (number) - Optional - Typical P sampling parameter. - **repeat_last_n** (integer) - Optional - The number of tokens to consider for `repeat_penalty`. - **temperature** (number) - Optional - The temperature for sampling. - **repeat_penalty** (number) - Optional - The penalty for repeating tokens. - **presence_penalty** (number) - Optional - The penalty for new tokens based on their presence. - **frequency_penalty** (number) - Optional - The penalty for new tokens based on their frequency. - **mirostat** (integer) - Optional - Mirostat sampling mode. - **mirostat_tau** (number) - Optional - Mirostat tau parameter. - **mirostat_eta** (number) - Optional - Mirostat eta parameter. - **penalize_newline** (boolean) - Optional - Whether to penalize newlines. - **stop** (array of strings) - Optional - Sequences where the API will stop generating further tokens. - **numa** (boolean) - Optional - Whether to use NUMA optimization. - **num_ctx** (integer) - Optional - The context window size. - **num_batch** (integer) - Optional - The batch size for prompt processing. - **num_gqa** (integer) - Optional - The number of GQA groups. - **num_gpu** (integer) - Optional - The number of GPUs to use. - **main_gpu** (integer) - Optional - The main GPU to use. - **low_vram** (boolean) - Optional - Whether to use low VRAM optimization. - **f16_kv** (boolean) - Optional - Whether to use FP16 for KV cache. - **vocab_only** (boolean) - Optional - Whether to only load the vocabulary. - **use_mmap** (boolean) - Optional - Whether to use memory mapping. - **use_mlock** (boolean) - Optional - Whether to use memory locking. - **rope_frequency_base** (number) - Optional - RoPE frequency base. - **rope_frequency_scale** (number) - Optional - RoPE frequency scale. - **num_thread** (integer) - Optional - The number of threads to use. ### Request Example (Raw Mode) ```shell curl http://localhost:11434/api/generate -d '{ "model": "mistral", "prompt": "[INST] why is the sky blue? [/INST]", "raw": true, "stream": false }' ``` ### Request Example (Reproducible Outputs) ```shell curl http://localhost:11434/api/generate -d '{ "model": "mistral", "prompt": "Why is the sky blue?", "options": { "seed": 123, "temperature": 0 } }' ``` ### Request Example (With Options) ```shell curl http://localhost:11434/api/generate -d '{ "model": "llama2", "prompt": "Why is the sky blue?", "stream": false, "options": { "num_keep": 5, "seed": 42, "num_predict": 100, "top_k": 20, "top_p": 0.9, "tfs_z": 0.5, "typical_p": 0.7, "repeat_last_n": 33, "temperature": 0.8, "repeat_penalty": 1.2, "presence_penalty": 1.5, "frequency_penalty": 1.0, "mirostat": 1, "mirostat_tau": 0.8, "mirostat_eta": 0.6, "penalize_newline": true, "stop": ["\n", "user:"], "numa": false, "num_ctx": 1024, "num_batch": 2, "num_gqa": 1, "num_gpu": 1, "main_gpu": 0, "low_vram": false, "f16_kv": true, "vocab_only": false, "use_mmap": true, "use_mlock": false, "rope_frequency_base": 1.1, "rope_frequency_scale": 0.8, "num_thread": 8 } }' ``` ### Request Example (Load a model) ```shell curl http://localhost:11434/api/generate -d '{ "model": "llama2" }' ``` ### Response #### Success Response (200) - **model** (string) - The name of the model used. - **created_at** (string) - The timestamp of model creation. - **response** (string) - The generated text response. - **done** (boolean) - Indicates if the generation is complete. - **context** (array of integers) - Optional. The context array if not in raw mode. - **total_duration** (integer) - Total duration of the request in nanoseconds. - **load_duration** (integer) - Duration of model loading in nanoseconds. - **prompt_eval_count** (integer) - Number of tokens evaluated in the prompt. - **prompt_eval_duration** (integer) - Duration of prompt evaluation in nanoseconds. - **eval_count** (integer) - Number of tokens evaluated in the response. - **eval_duration** (integer) - Duration of response evaluation in nanoseconds. #### Response Example (Standard) ```json { "model": "llava", "created_at": "2023-11-03T15:36:02.583064Z", "response": "A happy cartoon character, which is cute and cheerful.", "done": true, "context": [1, 2, 3], "total_duration": 2938432250, "load_duration": 2559292, "prompt_eval_count": 1, "prompt_eval_duration": 2195557000, "eval_count": 44, "eval_duration": 736432000 } ``` #### Response Example (Load a model) ```json { "model": "llama2", "created_at": "2023-12-18T19:52:07.071755Z", "response": "", "done": true } ``` ``` -------------------------------- ### Testing with Mock HTTP Clients in Kotlin Source: https://context7.com/tddworks/openai-kotlin/llms.txt Illustrates how to test code that depends on network requests by using a MockHttpClient. This involves setting up a mock response and integrating it with dependency injection (Koin) to provide a testable environment for verifying API interactions. ```kotlin import com.tddworks.common.network.api.ktor.api.HttpRequester import com.tddworks.common.network.api.ktor.api.mock.MockHttpClient import com.tddworks.openai.api.chat.api.ChatCompletion import org.koin.dsl.module // Create mock HTTP client for testing val mockHttpClient = MockHttpClient( response = """ { "id": "test-id", "object": "chat.completion", "created": 1234567890, "model": "gpt-3.5-turbo", "choices": [{ "index": 0, "message": { "role": "assistant", "content": "This is a test response" }, "finish_reason": "stop" }] } """.trimIndent() ) // Setup test module val testModule = module { single { mockHttpClient } } // Use in tests @Test fun `should return chat completion`() = runTest { val openAI = OpenAI.default(OpenAIConfig(apiKey = { "test-key" })) val response = openAI.chatCompletions( ChatCompletionRequest.from("test question") ) assertNotNull(response) assertEquals("This is a test response", response.choices[0].message.content) } ``` -------------------------------- ### Configure Anthropic Client with Custom Headers Source: https://github.com/tddworks/openai-kotlin/blob/main/README.md Demonstrates configuring the Anthropic client with a custom base URL, API key, and Anthropic API version. This allows for flexible integration with different Anthropic endpoints or versions. ```kotlin val anthropic = initAnthropic( AnthropicConfig( apiKey = { System.getenv("ANTHROPIC_API_KEY") }, anthropicVersion = { "2023-06-01" }, baseUrl = { "https://api.anthropic.com" } ) ) ```