Spaces:
Sleeping
Sleeping
File size: 1,671 Bytes
0079b8d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
from dotenv import load_dotenv
load_dotenv() # take environment variables from .env
import os
from openai import OpenAI
import vcr
client = OpenAI(
base_url=os.environ.get("OPENAI_API_BASE"),
api_key=os.environ.get("OPENAI_API_KEY"),
)
def completion(prompt, max_tokens=None, temperature=0):
_completion = client.completions.create(
model="gpt-3.5-turbo-instruct",
prompt=prompt,
max_tokens=max_tokens,
temperature=temperature,
)
return _completion.choices[0].text.strip()
def chat_completion(message, model="gpt-3.5-turbo", prompt=None, temperature=0):
# Initialize the messages list
messages = []
# Add the prompt to the messages list
if prompt is not None:
messages += [{"role": "system", "content": prompt}]
if message is not None:
# Add the user's message to the messages list
messages += [{"role": "user", "content": message}]
# Make an API call to the OpenAI ChatCompletion endpoint with the model and messages
_completion = client.chat.completions.create(
model=model, messages=messages, temperature=temperature
)
# Extract and return the AI's response from the API response
return _completion.choices[0].message.content.strip()
def __vcr():
return vcr.VCR(
serializer="yaml",
cassette_library_dir="tests/fixtures/cassettes",
record_mode="new_episodes",
match_on=["uri", "method", "path", "query", "body"],
record_on_exception=False,
)
def cassette_for(name):
filename = name + ".yaml"
return __vcr().use_cassette(filename, filter_headers=[("authorization", None)])
|