Skip to content

Commit

Permalink
Merge pull request #1 from sxflynn/updates
Browse files Browse the repository at this point in the history
Updated all examples to latest OpenAI package with new syntax, plus other upgrades
  • Loading branch information
developing-human committed Feb 23, 2024
2 parents 8b779ca + 8534a68 commit 46c681e
Show file tree
Hide file tree
Showing 75 changed files with 9,126 additions and 50,316 deletions.
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
OPENAI_API_KEY="your-key-here-in-quotes"
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.venv
venv
.env
__pycache__/
node_modules/
4 changes: 4 additions & 0 deletions 00_curl/curl_gpt.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
set -a
source ../.env
set +a

curl https://api.openai.com/v1/chat/completions \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-H "Content-Type: application/json" \
Expand Down
4 changes: 4 additions & 0 deletions 00_curl/curl_gpt_streaming.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
set -a
source ../.env
set +a

curl https://api.openai.com/v1/chat/completions \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-H "Content-Type: application/json" \
Expand Down
10 changes: 7 additions & 3 deletions 01_template/gpt_prompt.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import openai
import os
import sys
from openai import OpenAI
from dotenv import load_dotenv


def do_the_thing(arg1: str, arg2: str) -> str:
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
prompt = f"""Dear ChatGPT,
Please do the thing with {arg1} and {arg2}.
Expand All @@ -22,8 +25,9 @@ def do_the_thing(arg1: str, arg2: str) -> str:
exit(1)

# Exit early if the api key is not provided
openai.api_key = os.getenv("OPENAI_API_KEY")
if openai.api_key is None:
load_dotenv()
api_key = os.getenv("OPENAI_API_KEY")
if api_key is None:
print("Must define environment variable OPENAI_API_KEY")
exit(1)

Expand Down
25 changes: 13 additions & 12 deletions 02_script/gpt_prompt.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import openai
import os
import sys
import json
from openai import OpenAI
from dotenv import load_dotenv

class Suggestion:
def __init__(self, label, description):
Expand All @@ -11,6 +12,7 @@ def __init__(self, label, description):

# Uses ChatGPT to offer troubleshooting suggestions based on sound & location
def troubleshoot_car(sound: str, location: str) -> list[Suggestion]:
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
prompt = f"""Your goal is to help me troubleshoot a problem with my car.
I'm hearing a {sound} near {location}.
Suggest 3 ideas for determining the issue.
Expand All @@ -28,16 +30,14 @@ def troubleshoot_car(sound: str, location: str) -> list[Suggestion]:
"""

# Calls ChatGPT 3.5 with the above prompt.
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": prompt},
]
)
response = client.chat.completions.create(model="gpt-3.5-turbo",
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": prompt},
])

# Extracts the text content from the response.
response_content = response.choices[0].message["content"]
response_content = response.choices[0].message.content

# Parses the response into Suggestions and returns them
return parse_suggestions(response_content)
Expand Down Expand Up @@ -69,14 +69,15 @@ def parse_suggestions(text: str) -> list[Suggestion]:
exit(1)

# Exit early if the api key is not provided
openai.api_key = os.getenv("OPENAI_API_KEY")
if openai.api_key is None:
load_dotenv()
api_key = os.getenv("OPENAI_API_KEY")
if api_key is None:
print("Must define environment variable OPENAI_API_KEY")
exit(1)

sound = sys.argv[1]
location = sys.argv[2]

suggestions = troubleshoot_car(sound, location)
print(json.dumps([s.__dict__ for s in suggestions]))
print(json.dumps([s.__dict__ for s in suggestions], indent=4))

26 changes: 17 additions & 9 deletions 03_api/api.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import openai
import os
from fastapi import FastAPI
from pydantic import BaseModel
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel
from openai import OpenAI
from dotenv import load_dotenv

class Suggestion(BaseModel):
label: str
description: str

# Uses ChatGPT to offer troubleshooting suggestions based on sound & location
def troubleshoot_car(sound: str, location: str) -> list[Suggestion]:
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
prompt = f"""Your goal is to help me troubleshoot a problem with my car.
I'm hearing a {sound} near {location}.
Suggest 3 ideas for determining the issue.
Expand All @@ -27,16 +29,16 @@ def troubleshoot_car(sound: str, location: str) -> list[Suggestion]:
"""

# Calls ChatGPT 3.5 with the above prompt.
response = openai.ChatCompletion.create(
response = client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": prompt},
]
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": prompt},
],
)

# Extracts the text content from the response.
response_content = response.choices[0].message["content"]
response_content = response.choices[0].message.content

# Parses the response into Suggestions and returns them
return parse_suggestions(response_content)
Expand Down Expand Up @@ -65,15 +67,21 @@ def parse_suggestions(text: str) -> list[Suggestion]:
def create_app() -> FastAPI:
app = FastAPI()

@app.on_event("startup")
def startup_event():
clickable_url = "http://127.0.0.1:8000/search?sound=shuddering&location=wheels"
print(f"Try out the API: {clickable_url}")

@app.get("/search")
def search(sound: str, location: str) -> list[Suggestion]:
return troubleshoot_car(sound, location)

return app

# Exit early if the api key is not provided
openai.api_key = os.getenv("OPENAI_API_KEY")
if openai.api_key is None:
load_dotenv()
api_key = os.getenv("OPENAI_API_KEY")
if api_key is None:
print("Must define environment variable OPENAI_API_KEY")
exit(1)

Expand Down
File renamed without changes.
70 changes: 0 additions & 70 deletions 04_frontend/README.md

This file was deleted.

23 changes: 12 additions & 11 deletions 04_frontend/api.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import openai
import os
import sys
import json
from fastapi import FastAPI
from pydantic import BaseModel
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel
from openai import OpenAI
from dotenv import load_dotenv

class Suggestion(BaseModel):
label: str
description: str

# Uses ChatGPT to offer troubleshooting suggestions based on sound & location
def troubleshoot_car(sound: str, location: str) -> list[Suggestion]:
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
prompt = f"""Your goal is to help me troubleshoot a problem with my car.
I'm hearing a {sound} near {location}.
Suggest 3 ideas for determining the issue.
Expand All @@ -29,16 +29,16 @@ def troubleshoot_car(sound: str, location: str) -> list[Suggestion]:
"""

# Calls ChatGPT 3.5 with the above prompt.
response = openai.ChatCompletion.create(
response = client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": prompt},
]
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": prompt},
],
)

# Extracts the text content from the response.
response_content = response.choices[0].message["content"]
response_content = response.choices[0].message.content

# Parses the response into Suggestions and returns them
return parse_suggestions(response_content)
Expand Down Expand Up @@ -74,8 +74,9 @@ def search(sound: str, location: str) -> list[Suggestion]:
return app

# Exit early if the api key is not provided
openai.api_key = os.getenv("OPENAI_API_KEY")
if openai.api_key is None:
load_dotenv()
api_key = os.getenv("OPENAI_API_KEY")
if api_key is None:
print("Must define environment variable OPENAI_API_KEY")
exit(1)

Expand Down
13 changes: 13 additions & 0 deletions 04_frontend/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>04_frontend</title>
</head>
<body>
<div id="root"></div>
<script type="module" src="/src/main.jsx"></script>
</body>
</html>
Loading

0 comments on commit 46c681e

Please sign in to comment.