Skip to content

Commit

Permalink
fix: use configured model on each request
Browse files Browse the repository at this point in the history
  • Loading branch information
nalgeon committed Nov 11, 2023
1 parent 4cb751d commit 54b12cb
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 6 deletions.
10 changes: 6 additions & 4 deletions bot/ai/chatgpt.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""ChatGPT (GPT-3.5+) language model from OpenAI."""

import logging
from typing import Optional
from openai import AsyncAzureOpenAI, AsyncOpenAI
import tiktoken
from bot.config import config
Expand Down Expand Up @@ -33,25 +34,26 @@
class Model:
"""OpenAI API wrapper."""

def __init__(self, name: str) -> None:
def __init__(self, name: Optional[str]=None) -> None:
"""Creates a wrapper for a given OpenAI large language model."""
self.name = name

async def ask(self, question: str, history: list[tuple[str, str]]) -> str:
"""Asks the language model a question and returns an answer."""
# maximum number of input tokens
n_input = _calc_n_input(self.name, n_output=config.openai.params["max_tokens"])
model = self.name or config.openai.model
n_input = _calc_n_input(model, n_output=config.openai.params["max_tokens"])
messages = self._generate_messages(question, history)
messages = shorten(messages, length=n_input)
params = config.openai.params
logger.debug(
f"> chat request: model=%s, params=%s, messages=%s",
self.name,
model,
params,
messages,
)
resp = await openai.chat.completions.create(
model=self.name,
model=model,
messages=messages,
**params,
)
Expand Down
2 changes: 1 addition & 1 deletion bot/askers.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ async def reply(
class TextAsker(Asker):
"""Works with chat completion AI."""

model = ai.chatgpt.Model(config.openai.model)
model = ai.chatgpt.Model()

async def ask(self, question: str, history: list[tuple[str, str]]) -> str:
"""Asks AI a question."""
Expand Down
2 changes: 1 addition & 1 deletion tests/test_ai.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

class ModelTest(unittest.TestCase):
def setUp(self) -> None:
self.model = chatgpt.Model(config.openai.model)
self.model = chatgpt.Model()

def test_generate_messages(self):
history = [UserMessage("Hello", "Hi"), UserMessage("Is it cold today?", "Yep!")]
Expand Down

0 comments on commit 54b12cb

Please sign in to comment.