Home >Java >javaTutorial >How to use ChatGPT API to interact with Java?

How to use ChatGPT API to interact with Java?

PHPz
PHPzforward
2023-05-09 22:04:161110browse

Introduction

The OpenAI API can be applied to almost any task that involves understanding or generating natural language or code. We offer a range of models with different power levels for different tasks, with the ability to fine-tune your own custom model. These models can be used in everything from content generation to semantic search and classification.

Authentication

OpenAI API uses API keys for authentication. Visit your API keys page to retrieve the API key you will use in your requests.

Remember, your API key is secret! Do not share it with others or expose it in any client-side code (browser, application). Production requests must be routed through your own backend server, and your API keys can be securely loaded from environment variables or a key management service.

All API requests should include your API key in the AuthorizationHTTP header, as shown below:

Authorization: Bearer YOUR_API_KEY

For users who belong to multiple organizations, you can pass a header to specify Which organization is used for API requests. Usage from these API requests will count against the specified organization's subscription quota.

Curl Command Example:

curl https://api.openai.com/v1/models \
  -H 'Authorization: Bearer YOUR_API_KEY' \
  -H 'OpenAI-Organization: org-Kh417O0F3ISLtdXBdafrKQl2'

Making a Request

You can paste the command below into your terminal to run your first API request. Make sure to replace YOUR_API_KEY with your secret API key.

curl https://api.openai.com/v1/chat/completions \
  -H 'Content-Type: application/json' \
  -H 'Authorization: Bearer YOUR_API_KEY' \
  -d '{
  "model": "gpt-3.5-turbo",
  "messages": [{"role": "user", "content": "Say this is a test!"}],
  "temperature": 0.7
}'

This request queries the model to complete text that begins with the prompt "Say this is a testgpt-3.5-turbo". You should receive a response similar to the following:

{
   "id":"chatcmpl-abc123",
   "object":"chat.completion",
   "created":1677858242,
   "model":"gpt-3.5-turbo-0301",
   "usage":{
      "prompt_tokens":13,
      "completion_tokens":7,
      "total_tokens":20
   },
   "choices":[
      {
         "message":{
            "role":"assistant",
            "content":"\n\nThis is a test!"
         },
         "finish_reason":"stop",
         "index":0
      }
   ]
}

Now you have generated your first chat to completion. We can see finish_reasonisstop which means the API returned complete completion of model generation. In the above request, we only generated a single message, but you can set the parameter n to generate multiple message selections. In this example, gpt-3.5-turbo is used more for traditional text completion tasks. The model is also optimized for chat applications.

Create Chat

curl https://api.openai.com/v1/chat/completions \
  -H 'Content-Type: application/json' \
  -H 'Authorization: Bearer YOUR_API_KEY' \
  -d '{
  "model": "gpt-3.5-turbo",
  "messages": [{"role": "user", "content": "Hello!"}]
}'
{
  "model": "gpt-3.5-turbo",
  "messages": [{"role": "user", "content": "Hello!"}]
}
{
  "id": "chatcmpl-123",
  "object": "chat.completion",
  "created": 1677652288,
  "choices": [{
    "index": 0,
    "message": {
      "role": "assistant",
      "content": "\n\nHello there, how may I assist you today?",
    },
    "finish_reason": "stop"
  }],
  "usage": {
    "prompt_tokens": 9,
    "completion_tokens": 12,
    "total_tokens": 21
  }
}

PostMan Instance

How to use ChatGPT API to interact with Java?

The above is the detailed content of How to use ChatGPT API to interact with Java?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete