Skip to content

thedev-junyoung/openai-gpt-turorial

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

OpenAI GPT API Client

This library provides an easy-to-use Python interface for interacting with OpenAI's GPT models, including both the Assistant API and the Completion endpoint.

Features

  • Interactive GPT with Assistant
  • gpt request with Completions api
  • Cost calculation for api requests and responses from openai

Getting Started

  1. Clone the Repository: Clone this repository to your local machine:
git clone https://github.com/thedev-junyoung/GoogleDrivePyManager.git
  1. To get started with this library, you'll need to install it via pip and set up your OpenAI API key.
pip install openai python-dotenv
  1. create a .env file at the root of your project and include your OpenAI API key:
OPENAI_API_KEY='your-api-key-here'

Example Usage

This library supports both text completions using the Completion API and structured conversations with the Assistant API. Below are examples of how to use each feature.

Using the Completion API for Text Generation

To generate text completions, such as answering a trivia question, use the following code:

from dotenv import load_dotenv
from openai_api_client import OpenAICompletion  # Ensure you have this module in your project
import os

if __name__ == "__main__":
    # Load environment variables from the .env file
    load_dotenv()
    
    # Retrieve your OpenAI API key from the .env file
    OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
    
    # Initialize the completion service with the specified model
    service = OpenAICompletion(model_name="gpt-3.5-turbo", open_ai_key=OPENAI_API_KEY)
    
    # Provide the system prompt and user input
    system_prompt = "You are a helpful assistant designed to output JSON."
    user_input = "Who won the world series in 2020?"
    
    # Call the generate function to get the result
    result = service.generate(system_prompt, user_input)
    
    # Print the resulting text completion
    print(result)

Using the Completion API

The Completion API can be used to generate text completions for a variety of tasks.

from dotenv import load_dotenv
from legal_document_service import OpenAICompletion
import os

if __name__ == "__main__":
    # Load environment variables from the .env file
    load_dotenv()
    # Get API key from the .env file
    OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
    OpenAICompletion
    # Initialize the service with the chosen model
    service = OpenAICompletion(model_name="gpt-3.5-turbo", open_ai_key=OPENAI_API_KEY)
    
    # System prompt and user input for generating a legal document
    system_prompt = "You are a helpful assistant designed to output JSON."
    user_input = "Who won the world series in 2020?"
    
    # Generate the legal document
    result = service.generate(system_prompt, user_input)
    
    # Print the result
    print(result)

이미지 대체 텍스트

References

About

=== Run openai's tutorial quickly === === openai의 튜토리얼 빠르게 실행 ===

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages