Skip to content
Snippets Groups Projects
Unverified Commit c49a5e16 authored by Huu Le's avatar Huu Le Committed by GitHub
Browse files

chore: update wrong env name, add error handling for next question (#232)

parent 8b2de431
No related branches found
No related tags found
No related merge requests found
---
"create-llama": patch
---
Add error handling for generating the next question
---
"create-llama": patch
---
Fix wrong api key variable in Azure OpenAI provider
...@@ -311,7 +311,7 @@ const getModelEnvs = (modelConfig: ModelConfig): EnvVar[] => { ...@@ -311,7 +311,7 @@ const getModelEnvs = (modelConfig: ModelConfig): EnvVar[] => {
...(modelConfig.provider === "azure-openai" ...(modelConfig.provider === "azure-openai"
? [ ? [
{ {
name: "AZURE_OPENAI_KEY", name: "AZURE_OPENAI_API_KEY",
description: "The Azure OpenAI key to use.", description: "The Azure OpenAI key to use.",
value: modelConfig.apiKey, value: modelConfig.apiKey,
}, },
......
...@@ -9,6 +9,7 @@ const ALL_AZURE_OPENAI_CHAT_MODELS: Record<string, { openAIModel: string }> = { ...@@ -9,6 +9,7 @@ const ALL_AZURE_OPENAI_CHAT_MODELS: Record<string, { openAIModel: string }> = {
openAIModel: "gpt-3.5-turbo-16k", openAIModel: "gpt-3.5-turbo-16k",
}, },
"gpt-4o": { openAIModel: "gpt-4o" }, "gpt-4o": { openAIModel: "gpt-4o" },
"gpt-4o-mini": { openAIModel: "gpt-4o-mini" },
"gpt-4": { openAIModel: "gpt-4" }, "gpt-4": { openAIModel: "gpt-4" },
"gpt-4-32k": { openAIModel: "gpt-4-32k" }, "gpt-4-32k": { openAIModel: "gpt-4-32k" },
"gpt-4-turbo": { "gpt-4-turbo": {
...@@ -26,6 +27,9 @@ const ALL_AZURE_OPENAI_CHAT_MODELS: Record<string, { openAIModel: string }> = { ...@@ -26,6 +27,9 @@ const ALL_AZURE_OPENAI_CHAT_MODELS: Record<string, { openAIModel: string }> = {
"gpt-4o-2024-05-13": { "gpt-4o-2024-05-13": {
openAIModel: "gpt-4o-2024-05-13", openAIModel: "gpt-4o-2024-05-13",
}, },
"gpt-4o-mini-2024-07-18": {
openAIModel: "gpt-4o-mini-2024-07-18",
},
}; };
const ALL_AZURE_OPENAI_EMBEDDING_MODELS: Record< const ALL_AZURE_OPENAI_EMBEDDING_MODELS: Record<
...@@ -35,10 +39,6 @@ const ALL_AZURE_OPENAI_EMBEDDING_MODELS: Record< ...@@ -35,10 +39,6 @@ const ALL_AZURE_OPENAI_EMBEDDING_MODELS: Record<
openAIModel: string; openAIModel: string;
} }
> = { > = {
"text-embedding-ada-002": {
dimensions: 1536,
openAIModel: "text-embedding-ada-002",
},
"text-embedding-3-small": { "text-embedding-3-small": {
dimensions: 1536, dimensions: 1536,
openAIModel: "text-embedding-3-small", openAIModel: "text-embedding-3-small",
......
...@@ -33,8 +33,8 @@ export async function generateNextQuestions( ...@@ -33,8 +33,8 @@ export async function generateNextQuestions(
const questions = extractQuestions(response.text); const questions = extractQuestions(response.text);
return questions; return questions;
} catch (error) { } catch (error) {
console.error("Error: ", error); console.error("Error when generating the next questions: ", error);
throw error; return [];
} }
} }
......
...@@ -82,7 +82,7 @@ def init_azure_openai(): ...@@ -82,7 +82,7 @@ def init_azure_openai():
dimensions = os.getenv("EMBEDDING_DIM") dimensions = os.getenv("EMBEDDING_DIM")
azure_config = { azure_config = {
"api_key": os.environ["AZURE_OPENAI_KEY"], "api_key": os.environ["AZURE_OPENAI_API_KEY"],
"azure_endpoint": os.environ["AZURE_OPENAI_ENDPOINT"], "azure_endpoint": os.environ["AZURE_OPENAI_ENDPOINT"],
"api_version": os.getenv("AZURE_OPENAI_API_VERSION") "api_version": os.getenv("AZURE_OPENAI_API_VERSION")
or os.getenv("OPENAI_API_VERSION"), or os.getenv("OPENAI_API_VERSION"),
......
import logging
from typing import List from typing import List
from app.api.routers.models import Message from app.api.routers.models import Message
...@@ -9,11 +10,14 @@ NEXT_QUESTIONS_SUGGESTION_PROMPT = PromptTemplate( ...@@ -9,11 +10,14 @@ NEXT_QUESTIONS_SUGGESTION_PROMPT = PromptTemplate(
"You're a helpful assistant! Your task is to suggest the next question that user might ask. " "You're a helpful assistant! Your task is to suggest the next question that user might ask. "
"\nHere is the conversation history" "\nHere is the conversation history"
"\n---------------------\n{conversation}\n---------------------" "\n---------------------\n{conversation}\n---------------------"
"Given the conversation history, please give me $number_of_questions questions that you might ask next!" "Given the conversation history, please give me {number_of_questions} questions that you might ask next!"
) )
N_QUESTION_TO_GENERATE = 3 N_QUESTION_TO_GENERATE = 3
logger = logging.getLogger("uvicorn")
class NextQuestions(BaseModel): class NextQuestions(BaseModel):
"""A list of questions that user might ask next""" """A list of questions that user might ask next"""
...@@ -21,28 +25,37 @@ class NextQuestions(BaseModel): ...@@ -21,28 +25,37 @@ class NextQuestions(BaseModel):
class NextQuestionSuggestion: class NextQuestionSuggestion:
@staticmethod @staticmethod
async def suggest_next_questions( async def suggest_next_questions(
messages: List[Message], messages: List[Message],
number_of_questions: int = N_QUESTION_TO_GENERATE, number_of_questions: int = N_QUESTION_TO_GENERATE,
) -> List[str]: ) -> List[str]:
# Reduce the cost by only using the last two messages """
last_user_message = None Suggest the next questions that user might ask based on the conversation history
last_assistant_message = None Return as empty list if there is an error
for message in reversed(messages): """
if message.role == "user": try:
last_user_message = f"User: {message.content}" # Reduce the cost by only using the last two messages
elif message.role == "assistant": last_user_message = None
last_assistant_message = f"Assistant: {message.content}" last_assistant_message = None
if last_user_message and last_assistant_message: for message in reversed(messages):
break if message.role == "user":
conversation: str = f"{last_user_message}\n{last_assistant_message}" last_user_message = f"User: {message.content}"
elif message.role == "assistant":
output: NextQuestions = await Settings.llm.astructured_predict( last_assistant_message = f"Assistant: {message.content}"
NextQuestions, if last_user_message and last_assistant_message:
prompt=NEXT_QUESTIONS_SUGGESTION_PROMPT, break
conversation=conversation, conversation: str = f"{last_user_message}\n{last_assistant_message}"
nun_questions=number_of_questions,
) output: NextQuestions = await Settings.llm.astructured_predict(
NextQuestions,
return output.questions prompt=NEXT_QUESTIONS_SUGGESTION_PROMPT,
conversation=conversation,
number_of_questions=number_of_questions,
)
return output.questions
except Exception as e:
logger.error(f"Error when generating next question: {e}")
return []
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment