-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
wac: move OpenAI code to app.internal.openai
- Loading branch information
Showing
4 changed files
with
59 additions
and
56 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
from logging import getLogger | ||
|
||
from app.settings import get_settings | ||
|
||
|
||
FORCE_OPENAI_MODEL = None | ||
|
||
log = getLogger("WAS") | ||
settings = get_settings() | ||
|
||
if settings.openai_api_key is not None: | ||
log.info("Initializing OpenAI Client") | ||
import openai | ||
openai_client = openai.OpenAI( | ||
api_key=settings.openai_api_key, base_url=settings.openai_base_url) | ||
models = openai_client.models.list() | ||
if len(models.data) == 1: | ||
FORCE_OPENAI_MODEL = models.data[0].id | ||
log.info( | ||
f"Only one model on OpenAI endpoint - forcing model '{FORCE_OPENAI_MODEL}'") | ||
else: | ||
openai_client = None | ||
|
||
|
||
def openai_chat(text, model=settings.openai_model): | ||
log.info(f"OpenAI Chat request for text '{text}'") | ||
response = settings.command_not_found | ||
if FORCE_OPENAI_MODEL is not None: | ||
log.info(f"Forcing model '{FORCE_OPENAI_MODEL}'") | ||
model = FORCE_OPENAI_MODEL | ||
else: | ||
log.info(f"Using model '{model}'") | ||
if openai_client is not None: | ||
try: | ||
chat_completion = openai_client.chat.completions.create( | ||
messages=[ | ||
{ | ||
"role": "system", | ||
"content": settings.openai_system_prompt, | ||
}, | ||
{ | ||
"role": "user", | ||
"content": text, | ||
} | ||
], | ||
model=model, | ||
temperature=settings.openai_temperature, | ||
) | ||
response = chat_completion.choices[0].message.content | ||
# Make it friendly for TTS and display output | ||
response = response.replace('\n', ' ').replace('\r', '').lstrip() | ||
log.info(f"Got OpenAI response '{response}'") | ||
except Exception as e: | ||
log.info(f"OpenAI failed with '{e}") | ||
return response |
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