-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
133 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
openai | ||
llama-index | ||
llama-index-vector-stores-chroma | ||
pydantic | ||
numpy | ||
cohere | ||
tiktoken | ||
|
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,79 @@ | ||
import os | ||
import logging | ||
|
||
import instructor | ||
import openai | ||
from openai import OpenAI, AsyncOpenAI | ||
from dotenv import load_dotenv | ||
|
||
logger = logging.getLogger(__name__) | ||
logging.basicConfig(level=logging.INFO) | ||
|
||
load_dotenv(".env") | ||
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY") | ||
|
||
|
||
def api_function_call( | ||
system_message, | ||
query: str, | ||
model: str = "gpt-4-0125-preview", | ||
response_model=None, | ||
max_retries: int = 0, | ||
stream: bool = False, | ||
): | ||
|
||
client = instructor.patch(OpenAI()) | ||
try: | ||
message_data = { | ||
"model": model, | ||
"messages": [ | ||
{"role": "system", "content": system_message}, | ||
{"role": "user", "content": query}, | ||
], | ||
"max_retries": max_retries, | ||
"stream": stream, | ||
} | ||
if response_model is not None: | ||
message_data["response_model"] = response_model | ||
|
||
response = client.chat.completions.create(**message_data) | ||
error = False | ||
|
||
except openai.BadRequestError: | ||
error = True | ||
logger.exception("Invalid request to OpenAI API. See traceback:") | ||
error_message = ( | ||
"Something went wrong while connecting with OpenAI, try again soon!" | ||
) | ||
return error_message, error | ||
|
||
except openai.RateLimitError: | ||
error = True | ||
logger.exception("RateLimit error from OpenAI. See traceback:") | ||
error_message = "OpenAI servers seem to be overloaded, try again later!" | ||
return error_message, error | ||
|
||
except Exception as e: | ||
error = True | ||
logger.exception( | ||
"Some kind of error happened trying to generate the response. See traceback:" | ||
) | ||
error_message = ( | ||
"Something went wrong with connecting with OpenAI, try again soon!" | ||
) | ||
return error_message, error | ||
|
||
if stream is True and response_model is None: | ||
|
||
def answer_generator(): | ||
for chunk in response: | ||
token = chunk.choices[0].delta.content | ||
|
||
token = "" if token is None else token | ||
|
||
yield token | ||
|
||
return answer_generator(), error | ||
|
||
else: | ||
return response, error |
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