{"id":16378019,"url":"https://github.com/cjcrafter/chatgpt-java-api","last_synced_at":"2025-04-12T07:51:20.478Z","repository":{"id":147934214,"uuid":"609739470","full_name":"CJCrafter/ChatGPT-Java-API","owner":"CJCrafter","description":"Unofficial Java API supporting Chat, Assistants, and more!","archived":false,"fork":false,"pushed_at":"2024-09-24T19:56:10.000Z","size":1703,"stargazers_count":71,"open_issues_count":7,"forks_count":12,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-04-02T03:46:19.531Z","etag":null,"topics":["api","assistant-chat-bots","azure-openai","chatbot","chatgpt","chatgpt-api","chatgpt-bot","chatgpt-java","gpt","gpt-35-turbo","gpt-4","gpts","java","kotlin","openai","openai-api","openai-java","openai-kotlin"],"latest_commit_sha":null,"homepage":"https://openai.cjcrafter.com/","language":"Kotlin","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/CJCrafter.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":["CJCrafter"],"patreon":null,"open_collective":null,"ko_fi":null,"tidelift":null,"community_bridge":null,"liberapay":null,"issuehunt":null,"otechie":null,"lfx_crowdfunding":null,"custom":null}},"created_at":"2023-03-05T04:38:31.000Z","updated_at":"2025-03-28T13:20:24.000Z","dependencies_parsed_at":"2023-05-28T01:00:21.446Z","dependency_job_id":"3a1a9875-2c20-448a-aecd-fdbe550d6b4a","html_url":"https://github.com/CJCrafter/ChatGPT-Java-API","commit_stats":null,"previous_names":[],"tags_count":17,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CJCrafter%2FChatGPT-Java-API","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CJCrafter%2FChatGPT-Java-API/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CJCrafter%2FChatGPT-Java-API/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CJCrafter%2FChatGPT-Java-API/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/CJCrafter","download_url":"https://codeload.github.com/CJCrafter/ChatGPT-Java-API/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248537003,"owners_count":21120688,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["api","assistant-chat-bots","azure-openai","chatbot","chatgpt","chatgpt-api","chatgpt-bot","chatgpt-java","gpt","gpt-35-turbo","gpt-4","gpts","java","kotlin","openai","openai-api","openai-java","openai-kotlin"],"created_at":"2024-10-11T03:44:27.605Z","updated_at":"2025-04-12T07:51:20.438Z","avatar_url":"https://github.com/CJCrafter.png","language":"Kotlin","readme":"\u003cdiv align=\"center\"\u003e\n\n# ChatGPT Java API\n  [![Maven Central](https://img.shields.io/maven-central/v/com.cjcrafter/openai?color=blue\u0026label=Download)](https://central.sonatype.com/artifact/com.cjcrafter/openai)\n  [![](https://img.shields.io/badge/-docs%20-blueviolet?logo=Kotlin\u0026colorA=gray)](https://openai.cjcrafter.com/)\n  [![](https://img.shields.io/badge/-examples%20-orange?logo=Read+The+Docs\u0026colorA=gray)](https://github.com/CJCrafter/ChatGPT-Java-API/tree/master/examples/src/main)\n  [![](https://img.shields.io/github/discussions/CJCrafter/ChatGPT-Java-API)](https://github.com/CJCrafter/ChatGPT-Java-API/discussions)\n  [![License](https://img.shields.io/github/license/CJCrafter/ChatGPT-Java-API)](https://github.com/CJCrafter/ChatGPT-Java-API/blob/master/LICENSE)\n\nAn unofficial, easy-to-use Java/Kotlin OpenAI API for ChatGPT, Assistants, and more!\n\u003c/div\u003e\n\n## Features\n* [Completions](https://github.com/CJCrafter/ChatGPT-Java-API/blob/master/src/main/kotlin/com/cjcrafter/openai/OpenAI.kt#L44-L57)\n  * Streaming support via `OpenAI#streamCompletion`\n* [Chat Completions](https://github.com/CJCrafter/ChatGPT-Java-API/blob/master/src/main/kotlin/com/cjcrafter/openai/OpenAI.kt#L84-L92)\n  * Streaming support via `OpenAI#streamChatCompletion`\n  * Functions support, check out the [java examples](https://github.com/CJCrafter/ChatGPT-Java-API/blob/master/examples/src/main/java/chat/StreamChatCompletionFunction.java#L49) and [kotlin examples](https://github.com/CJCrafter/ChatGPT-Java-API/blob/master/examples/src/main/kotlin/chat/StreamChatCompletionFunction.kt#L37)\n  * [Azure Support](https://github.com/CJCrafter/ChatGPT-Java-API/blob/master/src/main/kotlin/com/cjcrafter/openai/OpenAI.kt#L271-L276) \n* [Embeddings](https://github.com/CJCrafter/ChatGPT-Java-API/blob/master/src/main/kotlin/com/cjcrafter/openai/OpenAI.kt#L113-L123) (New!)\n* [Files](https://github.com/CJCrafter/ChatGPT-Java-API/blob/master/src/main/kotlin/com/cjcrafter/openai/files/FileHandler.kt) (New!)\n* [Assistants](https://github.com/CJCrafter/ChatGPT-Java-API/blob/master/src/main/kotlin/com/cjcrafter/openai/assistants/AssistantHandler.kt) (New!)\n  * Which includes [Threads](), [Runs](), and everything else you'll need to run your assistants! \n\n## Installation\nFor Kotlin DSL (`build.gradle.kts`), add this to your dependencies block:\n```kotlin\ndependencies {\n    implementation(\"com.cjcrafter:openai:2.1.0\")\n}\n```\nFor Maven projects, add this to your `pom.xml` file in the `\u003cdependencies\u003e` block:\n```xml\n\u003cdependency\u003e\n    \u003cgroupId\u003ecom.cjcrafter\u003c/groupId\u003e\n    \u003cartifactId\u003eopenai\u003c/artifactId\u003e\n    \u003cversion\u003e2.1.0\u003c/version\u003e\n\u003c/dependency\u003e\n```\nSee the [maven repository](https://central.sonatype.com/artifact/com.cjcrafter/openai/2.1.0) for gradle/ant/etc.\n\n\n## Working Example\nThis is a simple working example of the ChatGPT API in Java:\n```java\n\nimport com.cjcrafter.openai.OpenAI;\nimport com.cjcrafter.openai.chat.ChatMessage;\nimport com.cjcrafter.openai.chat.ChatRequest;\nimport com.cjcrafter.openai.chat.ChatResponse;\nimport io.github.cdimascio.dotenv.Dotenv;\n\nimport java.util.ArrayList;\nimport java.util.List;\nimport java.util.Scanner;\n\n/**\n * In this Java example, we will be using the Chat API to create a simple chatbot.\n */\npublic class ChatCompletion {\n\n  public static void main(String[] args) {\n\n    // To use dotenv, you need to add the \"io.github.cdimascio:dotenv-kotlin:version\"\n    // dependency. Then you can add a .env file in your project directory.\n    String key = Dotenv.load().get(\"OPENAI_TOKEN\");\n    OpenAI openai = OpenAI.builder()\n            .apiKey(key)\n            .build();\n\n    List\u003cChatMessage\u003e messages = new ArrayList\u003c\u003e();\n    messages.add(ChatMessage.toSystemMessage(\"Help the user with their problem.\"));\n\n    // Here you can change the model's settings, add tools, and more.\n    ChatRequest request = ChatRequest.builder()\n            .model(\"gpt-3.5-turbo\")\n            .messages(messages)\n            .build();\n\n    Scanner scan = new Scanner(System.in);\n    while (true) {\n      System.out.println(\"What are you having trouble with?\");\n      String input = scan.nextLine();\n\n      messages.add(ChatMessage.toUserMessage(input));\n      ChatResponse response = openai.createChatCompletion(request);\n\n      System.out.println(\"Generating Response...\");\n      System.out.println(response.get(0).getMessage().getContent());\n\n      // Make sure to add the response to the messages list!\n      messages.add(response.get(0).getMessage());\n    }\n  }\n}\n```\n\nFor more examples, check out [examples](https://github.com/CJCrafter/ChatGPT-Java-API/tree/master/examples/src/main). \n\n\u003e **Note**: OpenAI recommends using environment variables for your API token \n([Read more](https://help.openai.com/en/articles/5112595-best-practices-for-api-key-safety)).\n\n## Logging\nWe use [SLF4J](http://www.slf4j.org/) for logging. To enable logging, add a logging implementation to your project.\nIf you encounter an issue with the JSON parsing, we will ask that you enable logging and send us the logs.\n\nAdding a logging implementation:\n```kotlin\nimplementation(\"ch.qos.logback:logback-classic:$version\")\n``` \n\nAdd a `logback.xml` file to your resources folder:\n```xml\n\u003cconfiguration\u003e\n    \u003cappender name=\"FILE\" class=\"ch.qos.logback.core.FileAppender\"\u003e\n        \u003cfile\u003edebug.log\u003c/file\u003e\n        \u003cappend\u003efalse\u003c/append\u003e\n        \u003cencoder\u003e\n            \u003cpattern\u003e%date %level [%thread] %logger{10} %msg%n\u003c/pattern\u003e\n        \u003c/encoder\u003e\n    \u003c/appender\u003e\n\n    \u003clogger name=\"com.cjcrafter.openai\" level=\"DEBUG\"/\u003e \u003c!-- Change to OFF to disable our logging --\u003e\n\n    \u003croot level=\"DEBUG\"\u003e\n        \u003cappender-ref ref=\"FILE\"/\u003e\n    \u003c/root\u003e\n\u003c/configuration\u003e\n```\n\n## Support\nIf I have saved you time, please consider [sponsoring me](https://github.com/sponsors/CJCrafter).\n\n## License\nChatGPT-Java-API is an open-sourced software licensed under the [MIT License](https://github.com/CJCrafter/ChatGPT-Java-API/blob/master/LICENSE).\n**This is an unofficial library, and is not affiliated with OpenAI**.\n","funding_links":["https://github.com/sponsors/CJCrafter"],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcjcrafter%2Fchatgpt-java-api","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcjcrafter%2Fchatgpt-java-api","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcjcrafter%2Fchatgpt-java-api/lists"}