Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

ChatGPT SDK for Java, CLI, GitHub Actions, demo, etc.

License

NotificationsYou must be signed in to change notification settings

kezhenxu94/chatgpt-java

Repository files navigation

Chat with ChatGPT Now.

ChatGPT Java SDK

Add the dependency

Maven metadata URL

Maven

<dependency>    <groupId>io.github.kezhenxu94</groupId>    <artifactId>chatgpt-java-sdk</artifactId>    <version>${chatgpt-java.version}</version></dependency>

Gradle

implementation'io.github.kezhenxu94:chatgpt-java-sdk:${chatgpt-java.version}'

Usage

publicclassChatGPTTest {publicstaticvoidmain(String[]args)throwsIOException,InterruptedException {finalvarchatGPT =ChatGPT                .builder()                .dataPath(Files.createTempDirectory("chatgpt"))// Persist the chat history to a data path                .build();// Start a new conversationfinalvarconversation =chatGPT.newConversation();System.out.println(conversation.ask("What's your name?").content());// Output: I'm an AI language model developed by OpenAI, and I don't have a name. What can I help you with today?System.out.println(conversation.ask("What did I ask you?").content());// Output: You asked for my name.conversation.save();// Save the history manually, conversations are saved on shutdown by default.finalvarconversation2 =chatGPT.newConversation("You are a software engineer.");System.out.println(conversation2.ask("What's your job?").content());System.out.println(conversation2.ask("What's your day to day work?").content());// Load a conversation by the IDfinalvarconversation3 =chatGPT.loadConversation(conversation.id());conversation3.ask("What did I ask you?");// Should print the same as the first conversation    }}

A demo usage of ChatGPT Java SDK is theChatGPT CLI, built with Spring Shell and Graalvm.

ChatGPT CLI

Who on earth would want to use CLI to interact with ChatGPT? Developers!

Prerequisites

An API key from OpenAI platform is required before using the CLI, headtoOpenAI Account to create one.

export CHATGPT_API_KEY=<your-api-key>

Download the CLI

Head toReleases to download the CLI according to your os.

sudo install chatgpt-cli.macos /usr/local/bin/chatgpt

Running the CLI in Docker

docker run -it --rm -e CHATGPT_API_KEY kezhenxu94/chatgpt-cli

Usage

Conversation (Interactive Mode)

chatgpt-cli by default runs in interactive mode and the questions in one session is a conversation, so ChatGPTremembersyour previous questions inside a single conversation, you can useask command to ask ChatGPT any question:

chatgpt> ask --question'What is your name?'

Or you can omit--question and simply:

chatgpt> ask"What is your name?"> ask"What did I ask you?"# should reply something like "You asked for my name."

But don't forget to quote the question if it contains spaces.

One-shot (Non-Interactive Mode)

If you want to ask ChatGPT a one-shot question, you can pass theask command and the question directly, so ChatGPTwillexit after it replies your questions:

chatgpt ask"What is your name?"

ChatGPT GitHub Action

Add OpenAI API Key Secret

Add your OpenAI API key as a secret to your repository.Go tohttps://github.com/<owner>/<repo>/settings/secrets/actions, create "New repository secret" with thenameOPENAI_KEY and your API key as the value.

Usage

-name:Setup ChatGPTuses:kezhenxu94/chatgpt-java/chatgpt-cli@v0.11.0-name:Answer the questionenv:CHATGPT_API_KEY:${{ secrets.OPENAI_KEY }}run:|    chatgpt ask "What is your name?"

[8]ページ先頭

©2009-2025 Movatter.jp