-
Notifications
You must be signed in to change notification settings - Fork 288
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: ✨ Intelligent caching keeps up with version gpt-1106 (#95)
* ✨ Intelligent caching keeps up with version gpt-1106 * deps fix
- Loading branch information
Showing
22 changed files
with
478 additions
and
278 deletions.
There are no files selected for viewing
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
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
Binary file not shown.
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
This file was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,9 @@ ssl/ | |
chat.yaml | ||
chat_*.yaml | ||
|
||
config.toml | ||
config_parser.py | ||
|
||
har/ | ||
Log/ | ||
Log-caloi-top/ | ||
|
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
from openai import OpenAI | ||
from openai._types import Headers, Query | ||
from rich import print | ||
from sparrow import MeasureTime, yaml_load # pip install sparrow-python | ||
|
||
config = yaml_load("config.yaml", rel_path=True) | ||
print(f"{config=}") | ||
|
||
client = OpenAI( | ||
api_key=config['api_key'], | ||
base_url=config['api_base'], | ||
) | ||
|
||
caching = True | ||
# caching = False | ||
stream = True | ||
|
||
json_obj_case = True | ||
function_case = True | ||
|
||
if json_obj_case: | ||
response = client.chat.completions.create( | ||
model="gpt-3.5-turbo-1106", | ||
response_format={"type": "json_object"}, | ||
messages=[ | ||
{ | ||
"role": "system", | ||
"content": "You are a helpful assistant designed to output JSON.", | ||
}, | ||
{"role": "user", "content": "Who won the world series in 2020?"}, | ||
], | ||
stream=stream, | ||
extra_body={"caching": caching}, | ||
) | ||
if stream: | ||
for chunk in response: | ||
print(chunk) | ||
else: | ||
print(response.choices[0].message.content) | ||
|
||
if function_case: | ||
tools = [ | ||
{ | ||
"type": "function", | ||
"function": { | ||
"name": "get_current_weather", | ||
"description": "Get the current weather in a given location", | ||
"parameters": { | ||
"type": "object", | ||
"properties": { | ||
"location": { | ||
"type": "string", | ||
"description": "The city and state, e.g. San Francisco, CA", | ||
}, | ||
"unit": {"type": "string", "enum": ["celsius", "fahrenheit"]}, | ||
}, | ||
"required": ["location"], | ||
}, | ||
}, | ||
} | ||
] | ||
messages = [{"role": "user", "content": "What's the weather like in Boston today?"}] | ||
completion = client.chat.completions.create( | ||
model="gpt-3.5-turbo", | ||
messages=messages, | ||
tools=tools, | ||
tool_choice="auto", | ||
stream=stream, | ||
extra_body={"caching": caching}, | ||
) | ||
|
||
if stream: | ||
for chunk in completion: | ||
print(chunk) | ||
else: | ||
print(completion) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
from openai import OpenAI | ||
from sparrow import relp, yaml_load | ||
|
||
config = yaml_load("config.yaml") | ||
|
||
client = OpenAI( | ||
base_url=config["api_base"], | ||
api_key=config["api_key"], | ||
) | ||
|
||
speech_file_path = relp("./speech.mp3") | ||
response = client.audio.speech.create( | ||
model="tts-1", | ||
voice="alloy", | ||
input="Today is a wonderful day to build something people love!", | ||
) | ||
|
||
response.stream_to_file(speech_file_path) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,13 @@ | ||
# Note: you need to be using OpenAI Python v0.27.0 for the code below to work | ||
import openai | ||
from openai import OpenAI | ||
from sparrow import relp, yaml_load | ||
|
||
config = yaml_load("config.yaml") | ||
openai.api_base = config["api_base"] | ||
openai.api_key = config["api_key"] | ||
audio_file = open(relp("../.github/data/whisper.m4a"), "rb") | ||
transcript = openai.Audio.transcribe("whisper-1", audio_file) | ||
|
||
client = OpenAI( | ||
base_url=config["api_base"], | ||
api_key=config["api_key"], | ||
) | ||
|
||
audio_file = open("/path/to/audio.mp3", "rb") | ||
transcript = client.audio.transcriptions.create(model="whisper-1", file=audio_file) | ||
print(transcript) |
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
Oops, something went wrong.