Tuesday, November 26, 2024

Seamless Integration: A Complete Guide to Using the ChatGPT API in Your Applications

 Seamless Integration: A Complete Guide to Using the ChatGPT API in Your Applications




Here’s how you can make a cURL request for generating text with specific keywords using the OpenAI API in Postman or directly via cURL:

1. cURL Request

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": "system",
        "content": "You are a professional writer skilled at creating engaging and informative content."
      },
      {
        "role": "user",
        "content": "Write a paragraph using the following keywords: AI, automation, efficiency. Make the text engaging and relevant to technology trends."
      }
    ],
    "max_tokens": 150,
    "temperature": 0.7
  }'

2. Import to Postman

Open Postman and create a new request.
Set the request method to POST and the URL to https://api.openai.com/v1/chat/completions.
Go to the Headers tab and add:
Content-Type: application/json
Authorization: Bearer your-api-key
Go to the Body tab, select raw, and paste the following JSON:

{
  "model": "gpt-3.5-turbo",
  "messages": [
    {
      "role": "system",
      "content": "You are a professional writer skilled at creating engaging and informative content."
    },
    {
      "role": "user",
      "content": "Write a paragraph using the following keywords: AI, automation, efficiency. Make the text engaging and relevant to technology trends."
    }
  ],
  "max_tokens": 150,
  "temperature": 0.7
}

3. Send the Request

Hit Send in Postman.
The response will contain the generated text under the choices[0].message.content field.

Example Response

{
  "id": "chatcmpl-abc123",
  "object": "chat.completion",
  "created": 1234567890,
  "choices": [
    {
      "message": {
        "role": "assistant",
        "content": "AI is transforming industries by enabling automation and boosting efficiency. From optimizing supply chains to enhancing customer experiences, these advancements pave the way for a smarter, more connected world."
      },
      "finish_reason": "stop",
      "index": 0
    }
  ],
  "usage": {
    "prompt_tokens": 50,
    "completion_tokens": 42,
    "total_tokens": 92
  }
}

Notes

Replace your-api-key with your actual OpenAI API key.
You can adjust temperature, max_tokens, and the messages field as needed.
This request works seamlessly in both cURL and Postman.

No comments:

Post a Comment

Understanding Essential DNS Record Types for Web Administrators

  Understanding Essential DNS Record Types for Web Administrators Introduction The Domain Name System (DNS) acts as the backbone of the inte...