From c49a5e162071053b83f9050ed4ace0149e201da3 Mon Sep 17 00:00:00 2001 From: Huu Le <39040748+leehuwuj@users.noreply.github.com> Date: Wed, 14 Aug 2024 14:39:14 +0700 Subject: [PATCH] chore: update wrong env name, add error handling for next question (#232) --- .changeset/kind-beans-sit.md | 5 ++ .changeset/ten-worms-pump.md | 5 ++ helpers/env-variables.ts | 2 +- helpers/providers/azure.ts | 8 +-- .../typescript/streaming/suggestion.ts | 4 +- .../components/settings/python/settings.py | 2 +- .../fastapi/app/api/services/suggestion.py | 55 ++++++++++++------- 7 files changed, 52 insertions(+), 29 deletions(-) create mode 100644 .changeset/kind-beans-sit.md create mode 100644 .changeset/ten-worms-pump.md diff --git a/.changeset/kind-beans-sit.md b/.changeset/kind-beans-sit.md new file mode 100644 index 00000000..05c562ec --- /dev/null +++ b/.changeset/kind-beans-sit.md @@ -0,0 +1,5 @@ +--- +"create-llama": patch +--- + +Add error handling for generating the next question diff --git a/.changeset/ten-worms-pump.md b/.changeset/ten-worms-pump.md new file mode 100644 index 00000000..c2c64711 --- /dev/null +++ b/.changeset/ten-worms-pump.md @@ -0,0 +1,5 @@ +--- +"create-llama": patch +--- + +Fix wrong api key variable in Azure OpenAI provider diff --git a/helpers/env-variables.ts b/helpers/env-variables.ts index e3f041ce..1ddd269c 100644 --- a/helpers/env-variables.ts +++ b/helpers/env-variables.ts @@ -311,7 +311,7 @@ const getModelEnvs = (modelConfig: ModelConfig): EnvVar[] => { ...(modelConfig.provider === "azure-openai" ? [ { - name: "AZURE_OPENAI_KEY", + name: "AZURE_OPENAI_API_KEY", description: "The Azure OpenAI key to use.", value: modelConfig.apiKey, }, diff --git a/helpers/providers/azure.ts b/helpers/providers/azure.ts index 74f90142..e450715f 100644 --- a/helpers/providers/azure.ts +++ b/helpers/providers/azure.ts @@ -9,6 +9,7 @@ const ALL_AZURE_OPENAI_CHAT_MODELS: Record<string, { openAIModel: string }> = { openAIModel: "gpt-3.5-turbo-16k", }, "gpt-4o": { openAIModel: "gpt-4o" }, + "gpt-4o-mini": { openAIModel: "gpt-4o-mini" }, "gpt-4": { openAIModel: "gpt-4" }, "gpt-4-32k": { openAIModel: "gpt-4-32k" }, "gpt-4-turbo": { @@ -26,6 +27,9 @@ const ALL_AZURE_OPENAI_CHAT_MODELS: Record<string, { openAIModel: string }> = { "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< @@ -35,10 +39,6 @@ const ALL_AZURE_OPENAI_EMBEDDING_MODELS: Record< openAIModel: string; } > = { - "text-embedding-ada-002": { - dimensions: 1536, - openAIModel: "text-embedding-ada-002", - }, "text-embedding-3-small": { dimensions: 1536, openAIModel: "text-embedding-3-small", diff --git a/templates/components/llamaindex/typescript/streaming/suggestion.ts b/templates/components/llamaindex/typescript/streaming/suggestion.ts index 83567111..0dacaead 100644 --- a/templates/components/llamaindex/typescript/streaming/suggestion.ts +++ b/templates/components/llamaindex/typescript/streaming/suggestion.ts @@ -33,8 +33,8 @@ export async function generateNextQuestions( const questions = extractQuestions(response.text); return questions; } catch (error) { - console.error("Error: ", error); - throw error; + console.error("Error when generating the next questions: ", error); + return []; } } diff --git a/templates/components/settings/python/settings.py b/templates/components/settings/python/settings.py index b723bf3e..bb828705 100644 --- a/templates/components/settings/python/settings.py +++ b/templates/components/settings/python/settings.py @@ -82,7 +82,7 @@ def init_azure_openai(): dimensions = os.getenv("EMBEDDING_DIM") azure_config = { - "api_key": os.environ["AZURE_OPENAI_KEY"], + "api_key": os.environ["AZURE_OPENAI_API_KEY"], "azure_endpoint": os.environ["AZURE_OPENAI_ENDPOINT"], "api_version": os.getenv("AZURE_OPENAI_API_VERSION") or os.getenv("OPENAI_API_VERSION"), diff --git a/templates/types/streaming/fastapi/app/api/services/suggestion.py b/templates/types/streaming/fastapi/app/api/services/suggestion.py index 406b0aec..ea563b17 100644 --- a/templates/types/streaming/fastapi/app/api/services/suggestion.py +++ b/templates/types/streaming/fastapi/app/api/services/suggestion.py @@ -1,3 +1,4 @@ +import logging from typing import List from app.api.routers.models import Message @@ -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. " "\nHere is the conversation history" "\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 +logger = logging.getLogger("uvicorn") + + class NextQuestions(BaseModel): """A list of questions that user might ask next""" @@ -21,28 +25,37 @@ class NextQuestions(BaseModel): class NextQuestionSuggestion: + @staticmethod async def suggest_next_questions( messages: List[Message], number_of_questions: int = N_QUESTION_TO_GENERATE, ) -> List[str]: - # Reduce the cost by only using the last two messages - last_user_message = None - last_assistant_message = None - for message in reversed(messages): - if message.role == "user": - last_user_message = f"User: {message.content}" - elif message.role == "assistant": - last_assistant_message = f"Assistant: {message.content}" - if last_user_message and last_assistant_message: - break - conversation: str = f"{last_user_message}\n{last_assistant_message}" - - output: NextQuestions = await Settings.llm.astructured_predict( - NextQuestions, - prompt=NEXT_QUESTIONS_SUGGESTION_PROMPT, - conversation=conversation, - nun_questions=number_of_questions, - ) - - return output.questions + """ + Suggest the next questions that user might ask based on the conversation history + Return as empty list if there is an error + """ + try: + # Reduce the cost by only using the last two messages + last_user_message = None + last_assistant_message = None + for message in reversed(messages): + if message.role == "user": + last_user_message = f"User: {message.content}" + elif message.role == "assistant": + last_assistant_message = f"Assistant: {message.content}" + if last_user_message and last_assistant_message: + break + conversation: str = f"{last_user_message}\n{last_assistant_message}" + + output: NextQuestions = await Settings.llm.astructured_predict( + NextQuestions, + 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 [] -- GitLab