-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
1,331 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
redis_url = "redis://localhost:6379/0" | ||
|
||
[[nodes]] | ||
node_name = "speaker" | ||
node_class = "speaker" | ||
|
||
|
||
[nodes.node_args] | ||
input_channel = "audio_input" | ||
|
||
[[nodes]] | ||
node_name = "listener" | ||
node_class = "listener" | ||
|
||
|
||
[nodes.node_args] | ||
output_channel = "audio_input" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
#!/usr/bin/env python3 | ||
from datetime import datetime | ||
from typing import List, Tuple | ||
from uuid import uuid4 | ||
|
||
from nicegui import ui | ||
|
||
messages: List[Tuple[str, str, str, str]] = [] | ||
|
||
|
||
@ui.refreshable | ||
def chat_messages(own_id: str) -> None: | ||
if messages: | ||
for user_id, avatar, text, stamp in messages: | ||
ui.chat_message( | ||
text=text, stamp=stamp, avatar=avatar, sent=own_id == user_id | ||
) | ||
else: | ||
ui.label("No messages yet").classes("mx-auto my-36") | ||
ui.run_javascript("window.scrollTo(0, document.body.scrollHeight)") | ||
|
||
|
||
@ui.page("/") | ||
async def main(): | ||
def send() -> None: | ||
stamp = datetime.utcnow().strftime("%X") | ||
messages.append((user_id, avatar, text.value, stamp)) | ||
text.value = "" | ||
chat_messages.refresh() | ||
|
||
user_id = str(uuid4()) | ||
avatar = f"https://robohash.org/{user_id}?bgset=bg2" | ||
|
||
ui.add_css( | ||
r"a:link, a:visited {color: inherit !important; text-decoration: none; font-weight: 500}" | ||
) | ||
with ui.footer().classes("bg-white"), ui.column().classes( | ||
"w-full max-w-3xl mx-auto my-6" | ||
): | ||
with ui.row().classes("w-full no-wrap items-center"): | ||
with ui.avatar().on("click", lambda: ui.navigate.to(main)): | ||
ui.image(avatar) | ||
text = ( | ||
ui.input(placeholder="message") | ||
.on("keydown.enter", send) | ||
.props("rounded outlined input-class=mx-3") | ||
.classes("flex-grow") | ||
) | ||
ui.markdown("simple chat app built with [NiceGUI](https://nicegui.io)").classes( | ||
"text-xs self-end mr-8 m-[-1em] text-primary" | ||
) | ||
|
||
await ( | ||
ui.context.client.connected() | ||
) # chat_messages(...) uses run_javascript which is only possible after connecting | ||
with ui.column().classes("w-full max-w-2xl mx-auto items-stretch"): | ||
chat_messages(user_id) | ||
|
||
|
||
if __name__ in {"__main__", "__mp_main__"}: | ||
ui.run(native=True) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
from typing import AsyncIterator | ||
from aact import Node, NodeFactory | ||
from aact.messages import Message, Text | ||
|
||
from openai import OpenAI | ||
|
||
|
||
@NodeFactory.register("gpt4_text_chatbot_node") | ||
class GPT4TextChatbotNode(Node[Text, Text]): | ||
def __init__(self, input_channel: str, output_channel: str, redis_url: str): | ||
super().__init__( | ||
input_channel_types=[(input_channel, Text)], | ||
output_channel_types=[(output_channel, Text)], | ||
redis_url=redis_url, | ||
) | ||
self.input_channel = input_channel | ||
self.output_channel = output_channel | ||
self.client = OpenAI() | ||
|
||
async def event_handler( | ||
self, channel: str, message: Message[Text] | ||
) -> AsyncIterator[tuple[str, Message[Text]]]: | ||
match channel: | ||
case self.input_channel: | ||
response = self.client.chat.completions.create( | ||
model="gpt-4o-mini", | ||
messages=[ | ||
{"role": "system", "content": "You are a helpful assistant."}, | ||
{"role": "user", "content": message.data.text}, | ||
], | ||
) | ||
yield ( | ||
self.output_channel, | ||
Message[Text](data=Text(text=response.choices[0].message.content)), | ||
) | ||
case _: | ||
raise ValueError(f"Unexpected channel: {channel}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
redis_url = "redis://localhost:6379/0" # required | ||
|
||
[[nodes]] | ||
node_name = "print" | ||
node_class = "print" | ||
|
||
[nodes.node_args.print_channel_types] | ||
"tick/secs/1" = "tick" | ||
|
||
[[nodes]] | ||
node_name = "tick" | ||
node_class = "tick" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
__version__ = "0.0.3" | ||
__version__ = "0.0.4" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
Oops, something went wrong.