Viroscope / Basic-Skeleton-OpenAI-Interface

This is a basic skeleton code for creating an interface to interact with OpenAI's GPT-3.5 language model. The code allows you to have a conversation with the model by sending prompts and receiving responses. Side Note: As a basic interface, this does not give the API memory.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Basic Skeleton OpenAI Interface

This is a basic skeleton code for creating an interface to interact with OpenAI's GPT-3.5 language model. The code allows you to have a conversation with the model by sending prompts and receiving responses.

Prerequisites

  • Python (version 3.6 or higher)
  • OpenAI Python package (openai)
  • dotenv Python package

Setup

  1. Install the required Python packages by running the following command:

    pip install openai python-dotenv
    
  2. Obtain an API key from OpenAI. Visit the OpenAI website for more information on how to get an API key.

  3. Create a file named .env in the same directory as the script and add the following line:

    OpenAIKey=YOUR_API_KEY
    

    Replace YOUR_API_KEY with your actual OpenAI API key.

Usage

  1. Import the required modules:

    import os
    from dotenv import load_dotenv
    import openai
  2. Load the API key from the .env file:

    load_dotenv()
    openai.api_key = os.getenv('OpenAIKey')
  3. Define the chat function:

    def chat(prompt):
        """
        Function for generating a chat-based completion using the OpenAI API.
    
        Args:
            prompt (str): The user's message or prompt.
    
        Returns:
            str: The assistant's reply.
        """
        response = openai.ChatCompletion.create(
            model="gpt-3.5-turbo-0613",
            messages=[
                {"role": "system", "content": "You are a helpful assistant."},
                {"role": "user", "content": prompt}
            ]
        )
    
        reply = response['choices'][0]['message']['content']
        return reply
  4. Start the conversation loop:

    while True:
        user_input = input("User: ")
        response = chat(user_input)
        print("Assistant:", response)
  5. Run the script and start interacting with the assistant.

Explanation

  1. The script begins by importing the necessary modules: os for working with environment variables, dotenv for loading the API key from the .env file, and openai for using the OpenAI API.

  2. The API key is loaded from the .env file using the load_dotenv() function and assigned to the openai.api_key variable.

  3. The chat function is defined, which takes a user's message or prompt as input and returns the assistant's reply. The function uses the openai.ChatCompletion.create() method to generate a chat-based completion based on the provided prompt and messages. The model parameter specifies the version of the GPT-3.5 model to use, and the messages parameter is a list of messages exchanged between the system and the user. The function extracts the assistant's reply from the API response and returns it.

  4. The script enters a loop where it prompts the user for input, calls the chat function to get the assistant's reply, and then prints the assistant's reply.

  5. The loop continues indefinitely until the program is terminated.

Note: This code uses the GPT-3.5 Turbo model (gpt-3.5-turbo-0613). You can change the model to a different version if desired, but keep in mind that different models may have different capabilities and cost structures.

About

This is a basic skeleton code for creating an interface to interact with OpenAI's GPT-3.5 language model. The code allows you to have a conversation with the model by sending prompts and receiving responses. Side Note: As a basic interface, this does not give the API memory.


Languages

Language:Python 100.0%