Skip to content

Instantly share code, notes, and snippets.

Last active May 2, 2023 03:44
Show Gist options
  • Save ozbillwang/0a73c10499b60df3e1be84e769d97462 to your computer and use it in GitHub Desktop.
Save ozbillwang/0a73c10499b60df3e1be84e769d97462 to your computer and use it in GitHub Desktop.
Run as text mode of with model `gpt-3.5-turbo` via API
A python code runs as text mode of with model `gpt-3.5-turbo` via API
### Usage
1. install openai pythonpackage
pip install openai python-dotenv
2. get the OpenAI API Key
3. set environment variable
export OPENAI_API_KEY=sk-xxxxxxxx
Or you can put the key in local `.env` file
$ cat .env
4. run below codes, play with GPT-3.5
#!/usr/bin/env python3
import os
import openai
from dotenv import load_dotenv
# Load environment variables from .env file
# Set OpenAI API key from environment variable
openai.api_key = os.getenv("OPENAI_API_KEY")
# Set GPT-3.5 model ID
model_id = 'gpt-3.5-turbo'
# Define function to send a message to the OpenAI API and get a response
def chat_gpt_conversation(conversation):
# Send the conversation to the OpenAI API and get a response
response = openai.ChatCompletion.create(
# Add the assistant's message to the conversation
'role': response.choices[0].message.role,
'content': response.choices[0].message.content
# Return the updated conversation
return conversation
# Define function to display a message from the conversation
def display_message(message):
print(f"{message['role'].capitalize()}: {message['content'].strip()}\n")
# Initialize an empty conversation
# Start the conversation loop
while True:
# Get user input
user_input = input('User: ')
# Add the user's message to the conversation
conversation.append({'role': 'user', 'content': user_input})
# Send the conversation to the OpenAI API and get a response
conversation = chat_gpt_conversation(conversation)
# Display the assistant's response
$ python
User: Write a blog post about the importance of productivity for small businesses
Assistant: As a small business owner, it’s important to always be on the hunt for ways to improve your business, increase revenue, and reach your goals more efficiently. One of the key ways to achieve these targets is to focus on productivity. Productivity is vital for the success of any business – large or small. It ensures that you are able to maximize your resources and stay competitive in today’s fast-paced market. Here are some key elements that illustrate the importance of productivity for small businesses.

Efficient utilization of resources


User: I want to bake a cake but don't know what temperature to set the oven to.
Assistant: The oven temperature for baking a cake will depend on the type of cake and the size of the tin you are using. Here are some general guidelines to help you set the right temperature for your cake:

1. For sponge cakes, which contain a lot of eggs and sugar, preheat your oven to 350°F (180°C). This temperature works well for most sponge cakes, regardless of their size.

2. For chocolate cakes, which contain cocoa powder and sometimes melted chocolate, set your oven to 325°F (160°C) to ensure that the chocolate does not burn.


Copy link

ozbillwang commented May 2, 2023

another nice package openai2 you can use to simplify the codes with same functions.

import os
from openai2 import Chat

api_key = os.getenv("OPENAI_API_KEY")

talk = Chat(api_key=api_key,model="gpt-3.5-turbo")

while True:
    Assistant = talk.request(input('User: '))
    print(f"Assistant: {Assistant}\n")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment