OpenAI API Integration 

At Sreyas IT Solutions, we specialize in delivering advanced AI-powered solutions tailored to meet diverse business needs. Our expertise in integrating APIs, such as OpenAI’s robust language models, ensures seamless implementation for generating intelligent and contextually relevant responses. With a proven track record in deploying cutting-edge technologies, Sreyas stands out in providing customized, scalable, and reliable solutions that empower businesses to achieve their objectives. This document outlines the detailed process for integrating OpenAI’s API to generate exam question responses. It includes information on API endpoint usage, request structure, and expected responses, showing Sreyas’ commitment to precision and efficiency.

API Integration Details

OpenAI API Endpoint

  • Endpoint: https://api.openai.com/v1/completions
  • Method: POST
  • Description: Generates responses based on a given prompt using OpenAI’s language model.

Request Structure

Request Headers

Set the following headers in your request:


{
  "Content-Type": "application/json",
  "Authorization": "Bearer YOUR_OPENAI_API_KEY"
}
  • Content-Type: Specifies the media type of the resource (application/json).
  • Authorization: Bearer token for authentication with OpenAI API. Replace YOUR_OPENAI_API_KEY with your actual API key.

 Request Body

The body of the POST request should be in JSON format and include the following fields:

{
  "model": "gpt-3.5-turbo-instruct",
  "prompt": "Exam Name: [exam_name]\nExam Description: [exam_description]\nQuestion: [question_text]",
  "max_tokens": 250,
  "temperature": 0.7
}
  • model: Specifies the model to be used. Example: “gpt-3.5-turbo-instruct”.
  • prompt: The prompt string that combines the exam name, description, and question text.
    • Exam Name: The name of the exam.
    • Exam Description: Detailed description of the exam.
    • Question: The specific question to be answered.
  • max_tokens: Maximum number of tokens to generate in the response (e.g., 250).
  • temperature: Controls the randomness of the response (0.7 is a common value).

Example Request

JSON Request Body

Here is an example request body with specific values:

{
  "model": "gpt-3.5-turbo-instruct",
  "prompt": "Exam Name: Mathematics\nExam Description: Addition is a fundamental arithmetic operation in mathematics. It involves combining two or more numbers to obtain a total or sum.\nQuestion: What is the total sum of 1 + 1?",
  "max_tokens": 250,
  "temperature": 0.7
}

Expected Response

Response Structure

The response from the OpenAI API will be in JSON format and include the following fields:

{
  "id": "cmpl-9qvmlsqbs9XKYlTFhvXDycwcWcaVK",
  "object": "text_completion",
  "created": 1722402211,
  "model": "gpt-3.5-turbo-instruct",
  "choices": [
    {
      "text": "\n\nThe total sum of 1 + 1 is 2.",
      "index": 0,
      "logprobs": null,
      "finish_reason": "stop"
    }
  ],
  "usage": {
    "prompt_tokens": 38,
    "completion_tokens": 14,
    "total_tokens": 52
  }
}
  • object: Type of response object (text_completion).
  • created: Timestamp of when the completion was created.
  • model: Model used for the completion.
  • choices: List of completion choices, each including:
    • text: The generated response text.
    • index: Position in the list of choices.
    • logprobs: Log probabilities of the tokens (null in this case).
    • finish_reason: Reason for stopping the generation (e.g., “stop”).
  • usage: Information about token usage:
    • prompt_tokens: Number of tokens used for the prompt.
    • completion_tokens: Number of tokens used for the completion.
    • total_tokens: Total number of tokens used.

Notes

  • Replace YOUR_OPENAI_API_KEY with your actual OpenAI API key.
  • Adjust max_tokens and temperature parameters based on your specific needs and desired response quality.

Sreyas has consistently delivered excellence in API integrations, empowering organizations to leverage AI technologies effectively. We ensure that each project is executed with precision, catering to unique requirements and delivering tangible results. Partner with Sreyas to transform your business processes and achieve innovative outcomes seamlessly.

Recent Blogs


Posted

in

by

Tags:

To Know Us Better

Browse through our work.

Explore The Technology Used

Learn about the cutting-edge technology and techniques we use to create innovative software solutions.