feat: swap openai for Groq via openai-compatible base_url
This commit is contained in:
parent
377016478e
commit
7f5ca81c71
3 changed files with 22 additions and 25 deletions
|
|
@ -20,5 +20,7 @@ CHROME_PATH = 'C:/Program Files (x86)/Google/Chrome/Application/chrome.exe %s'
|
|||
# Токен Picovoice
|
||||
PICOVOICE_TOKEN = os.getenv('PICOVOICE_TOKEN')
|
||||
|
||||
# Токен OpenAI
|
||||
OPENAI_TOKEN = os.getenv('OPENAI_TOKEN')
|
||||
# Токен Groq
|
||||
GROQ_TOKEN = os.getenv('GROQ_TOKEN')
|
||||
GROQ_BASE_URL = "https://api.groq.com/openai/v1"
|
||||
GROQ_MODEL = "llama-3.3-70b-versatile"
|
||||
|
|
|
|||
2
dev.env
2
dev.env
|
|
@ -1,2 +1,2 @@
|
|||
PICOVOICE_TOKEN=<тут ключ-picovoice>
|
||||
OPENAI_TOKEN=<тут ключ-openai>
|
||||
GROQ_TOKEN=<тут ключ-groq>
|
||||
|
|
|
|||
39
main.py
39
main.py
|
|
@ -10,7 +10,7 @@ import time
|
|||
from ctypes import POINTER, cast
|
||||
|
||||
import openai
|
||||
from openai import error
|
||||
from openai import OpenAI
|
||||
import pvporcupine
|
||||
import simpleaudio as sa
|
||||
import vosk
|
||||
|
|
@ -37,8 +37,7 @@ VA_CMD_LIST = yaml.safe_load(
|
|||
system_message = {"role": "system", "content": "Ты голосовой ассистент из железного человека."}
|
||||
message_log = [system_message]
|
||||
|
||||
# init openai
|
||||
openai.api_key = config.OPENAI_TOKEN
|
||||
client = OpenAI(api_key=config.GROQ_TOKEN, base_url=config.GROQ_BASE_URL)
|
||||
|
||||
# PORCUPINE
|
||||
porcupine = pvporcupine.create(
|
||||
|
|
@ -59,33 +58,29 @@ q = queue.Queue()
|
|||
def gpt_answer():
|
||||
global message_log
|
||||
|
||||
model_engine = "gpt-3.5-turbo"
|
||||
max_tokens = 256 # default 1024
|
||||
try:
|
||||
response = openai.ChatCompletion.create(
|
||||
model=model_engine,
|
||||
response = client.chat.completions.create(
|
||||
model=config.GROQ_MODEL,
|
||||
messages=message_log,
|
||||
max_tokens=max_tokens,
|
||||
max_tokens=256,
|
||||
temperature=0.7,
|
||||
top_p=1,
|
||||
stop=None
|
||||
stop=None,
|
||||
)
|
||||
except (error.TryAgain, error.ServiceUnavailableError):
|
||||
return "ChatGPT перегружен!"
|
||||
except openai.OpenAIError as ex:
|
||||
# если ошибка - это макс длина контекста, то возвращаем ответ с очищенным контекстом
|
||||
if ex.code == "context_length_exceeded":
|
||||
except openai.BadRequestError as ex:
|
||||
code = getattr(ex, "code", None) or ""
|
||||
message = str(ex)
|
||||
if code == "context_length_exceeded" or "context_length_exceeded" in message:
|
||||
message_log = [system_message, message_log[-1]]
|
||||
return gpt_answer()
|
||||
else:
|
||||
return "OpenAI токен не рабочий."
|
||||
return "Запрос отклонён моделью."
|
||||
except openai.RateLimitError:
|
||||
return "Лимит запросов исчерпан."
|
||||
except openai.APIConnectionError:
|
||||
return "Не могу связаться с сервером."
|
||||
except openai.APIError:
|
||||
return "Groq токен не рабочий."
|
||||
|
||||
# Find the first response from the chatbot that has text in it (some responses may not have text)
|
||||
for choice in response.choices:
|
||||
if "text" in choice:
|
||||
return choice.text
|
||||
|
||||
# If no response with text is found, return the first response's content (which may be empty)
|
||||
return response.choices[0].message.content
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue