From 614679ea1531d5568c4869cf7dc32948eee6a27b Mon Sep 17 00:00:00 2001 From: Pankaj <129259520+PraveenRepswal@users.noreply.github.com> Date: Sun, 4 Jan 2026 22:41:01 +0530 Subject: [PATCH 1/2] Fix issue #255 - Sample replies for chatbot --- .../conversation.py | 21 +++++++++++-------- .../pages/chat_page.py | 7 ++++++- .../project-template-AI Chatbot/root_init.py | 8 +++++-- 3 files changed, 24 insertions(+), 12 deletions(-) diff --git a/rio/snippets/snippet-files/project-template-AI Chatbot/conversation.py b/rio/snippets/snippet-files/project-template-AI Chatbot/conversation.py index fb54d74f..eb7f87e1 100644 --- a/rio/snippets/snippet-files/project-template-AI Chatbot/conversation.py +++ b/rio/snippets/snippet-files/project-template-AI Chatbot/conversation.py @@ -27,7 +27,7 @@ class Conversation: # The entire message history messages: list[ChatMessage] = dataclasses.field(default_factory=list) - async def respond(self, client: openai.AsyncOpenAI) -> ChatMessage: + async def respond(self, client: openai.AsyncOpenAI | None) -> ChatMessage: """ Creates an AI generated response for this conversation and appends it to the messages list. Also returns the new message. @@ -56,18 +56,21 @@ class Conversation: ] # Generate a response - api_response = await client.chat.completions.create( - model="gpt-3.5-turbo", - messages=api_messages, - max_tokens=500, - ) - - assert isinstance(api_response.choices[0].message.content, str) + if client is None: + response_text = ("You need to add an OpenAI API key") + else: + api_response = await client.chat.completions.create( + model="gpt-3.5-turbo", + messages=api_messages, + max_tokens=500, + ) + assert isinstance(api_response.choices[0].message.content, str) + response_text = api_response.choices[0].message.content response = ChatMessage( role="assistant", timestamp=datetime.now(tz=timezone.utc), - text=api_response.choices[0].message.content, + text=response_text, ) # Append the message and return it as well diff --git a/rio/snippets/snippet-files/project-template-AI Chatbot/pages/chat_page.py b/rio/snippets/snippet-files/project-template-AI Chatbot/pages/chat_page.py index 27ff000d..67c9c0a3 100644 --- a/rio/snippets/snippet-files/project-template-AI Chatbot/pages/chat_page.py +++ b/rio/snippets/snippet-files/project-template-AI Chatbot/pages/chat_page.py @@ -81,10 +81,15 @@ class ChatPage(rio.Component): self.is_loading = True self.force_refresh() + try: + openai_client = self.session[openai.AsyncOpenAI] + except KeyError: + openai_client = None + # Generate a response try: await self.conversation.respond( - client=self.session[openai.AsyncOpenAI], + client=openai_client, ) # Don't get stuck in loading state if an error occurs diff --git a/rio/snippets/snippet-files/project-template-AI Chatbot/root_init.py b/rio/snippets/snippet-files/project-template-AI Chatbot/root_init.py index 75a61b26..7a43e040 100644 --- a/rio/snippets/snippet-files/project-template-AI Chatbot/root_init.py +++ b/rio/snippets/snippet-files/project-template-AI Chatbot/root_init.py @@ -22,12 +22,16 @@ Make sure to enter your key into the `__init__.py` file before trying to run the """.strip() print(message) - raise RuntimeError(message) + # raise RuntimeError(message) + OPENAI_CLIENT = None +else: + OPENAI_CLIENT = openai.AsyncOpenAI(api_key=OPENAI_API_KEY) def on_app_start(app: rio.App) -> None: # Create the OpenAI client and attach it to the app - app.default_attachments.append(openai.AsyncOpenAI(api_key=OPENAI_API_KEY)) + if OPENAI_CLIENT is not None: + app.default_attachments.append(OPENAI_CLIENT) # From b3534c494985b3c8fccc2f8ad4dba13b5ca33a8d Mon Sep 17 00:00:00 2001 From: Jakob Pinterits Date: Thu, 8 Jan 2026 19:43:25 +0100 Subject: [PATCH 2/2] tweaks to error message & comment --- .../project-template-AI Chatbot/conversation.py | 7 ++++++- .../project-template-AI Chatbot/root_init.py | 11 +++++------ 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/rio/snippets/snippet-files/project-template-AI Chatbot/conversation.py b/rio/snippets/snippet-files/project-template-AI Chatbot/conversation.py index eb7f87e1..1144d70f 100644 --- a/rio/snippets/snippet-files/project-template-AI Chatbot/conversation.py +++ b/rio/snippets/snippet-files/project-template-AI Chatbot/conversation.py @@ -57,7 +57,12 @@ class Conversation: # Generate a response if client is None: - response_text = ("You need to add an OpenAI API key") + response_text = """ +This template requires an OpenAI API key to work + +You can get your API key from https://platform.openai.com/api-keys +Make sure to enter your key into the `__init__.py` file before trying to run the project. + """.strip() else: api_response = await client.chat.completions.create( model="gpt-3.5-turbo", diff --git a/rio/snippets/snippet-files/project-template-AI Chatbot/root_init.py b/rio/snippets/snippet-files/project-template-AI Chatbot/root_init.py index 7a43e040..b57c88de 100644 --- a/rio/snippets/snippet-files/project-template-AI Chatbot/root_init.py +++ b/rio/snippets/snippet-files/project-template-AI Chatbot/root_init.py @@ -14,15 +14,14 @@ OPENAI_API_KEY = "" # Replace this with your OpenAI API key # yet. Feel free to delete this code if you've already replaced the key. if OPENAI_API_KEY == "": - message = """ + print( + """ This template requires an OpenAI API key to work -You can get your API key from [OpenAI's website](https://platform.openai.com/api-keys -Make sure to enter your key into the `__init__.py` file before trying to run the project +You can get your API key from https://platform.openai.com/api-keys +Make sure to enter your key into the `__init__.py` file before trying to run the project. """.strip() - - print(message) - # raise RuntimeError(message) + ) OPENAI_CLIENT = None else: OPENAI_CLIENT = openai.AsyncOpenAI(api_key=OPENAI_API_KEY)