$ python gpt-3.5.py
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.
...
User:
Last active
May 2, 2023 03:44
-
-
Save ozbillwang/0a73c10499b60df3e1be84e769d97462 to your computer and use it in GitHub Desktop.
Run as text mode of https://chat.openai.com with model `gpt-3.5-turbo` via API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
A python code runs as text mode of https://chat.openai.com with model `gpt-3.5-turbo` via API | |
### Usage | |
1. install openai pythonpackage | |
``` | |
pip install openai python-dotenv | |
``` | |
2. get the OpenAI API Key | |
ref: https://platform.openai.com/account/api-keys | |
3. set environment variable | |
``` | |
export OPENAI_API_KEY=sk-xxxxxxxx | |
``` | |
Or you can put the key in local `.env` file | |
``` | |
$ cat .env | |
OPENAI_API_KEY=sk-xxxxxx | |
``` | |
4. run below codes, play with GPT-3.5 | |
``` | |
python gpt-3.5.py | |
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import os | |
import openai | |
from dotenv import load_dotenv | |
# Load environment variables from .env file | |
load_dotenv() | |
# 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( | |
model=model_id, | |
messages=conversation | |
) | |
# Add the assistant's message to the conversation | |
conversation.append({ | |
'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 | |
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 | |
display_message(conversation[-1]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
another nice package openai2 you can use to simplify the codes with same functions.