|
| 1 | +""" |
| 2 | +A simple example of chatting to an LLM with Textual. |
| 3 | +
|
| 4 | +Lots of room for improvement here. |
| 5 | +
|
| 6 | +See https://textual.textualize.io/blog/2024/09/15/anatomy-of-a-textual-user-interface/ |
| 7 | +
|
| 8 | +""" |
| 9 | + |
| 10 | +# /// script |
| 11 | +# requires-python = ">=3.12" |
| 12 | +# dependencies = [ |
| 13 | +# "llm", |
| 14 | +# "textual", |
| 15 | +# ] |
| 16 | +# /// |
| 17 | +from textual import on, work |
| 18 | +from textual.app import App, ComposeResult |
| 19 | +from textual.containers import VerticalScroll |
| 20 | +from textual.widgets import Footer, Header, Input, Markdown |
| 21 | + |
| 22 | +try: |
| 23 | + import llm |
| 24 | +except ImportError: |
| 25 | + raise ImportError("install the 'llm' package or run with 'uv run mother.py'") |
| 26 | + |
| 27 | +# The system prompt |
| 28 | +SYSTEM = """Formulate all responses as if you where the sentient AI named Mother from the Alien movies.""" |
| 29 | + |
| 30 | + |
| 31 | +class Prompt(Markdown): |
| 32 | + """Markdown for the user prompt.""" |
| 33 | + |
| 34 | + |
| 35 | +class Response(Markdown): |
| 36 | + """Markdown for the reply from the LLM.""" |
| 37 | + |
| 38 | + BORDER_TITLE = "Mother" |
| 39 | + |
| 40 | + |
| 41 | +class MotherApp(App): |
| 42 | + """Simple app to demonstrate chatting to an LLM.""" |
| 43 | + |
| 44 | + AUTO_FOCUS = "Input" |
| 45 | + |
| 46 | + CSS = """ |
| 47 | + Prompt { |
| 48 | + background: $primary 10%; |
| 49 | + color: $text; |
| 50 | + margin: 1; |
| 51 | + margin-right: 8; |
| 52 | + padding: 1 2 0 2; |
| 53 | + } |
| 54 | +
|
| 55 | + Response { |
| 56 | + border: wide $success; |
| 57 | + background: $success 10%; |
| 58 | + color: $text; |
| 59 | + margin: 1; |
| 60 | + margin-left: 8; |
| 61 | + padding: 1 2 0 2; |
| 62 | + } |
| 63 | + """ |
| 64 | + |
| 65 | + def compose(self) -> ComposeResult: |
| 66 | + yield Header() |
| 67 | + with VerticalScroll(id="chat-view"): |
| 68 | + yield Response("INTERFACE 2037 READY FOR INQUIRY") |
| 69 | + yield Input(placeholder="How can I help you?") |
| 70 | + yield Footer() |
| 71 | + |
| 72 | + def on_mount(self) -> None: |
| 73 | + """You might want to change the model if you don't have access to it.""" |
| 74 | + self.model = llm.get_model("gpt-4o") |
| 75 | + |
| 76 | + @on(Input.Submitted) |
| 77 | + async def on_input(self, event: Input.Submitted) -> None: |
| 78 | + """When the user hits return.""" |
| 79 | + chat_view = self.query_one("#chat-view") |
| 80 | + event.input.clear() |
| 81 | + await chat_view.mount(Prompt(event.value)) |
| 82 | + await chat_view.mount(response := Response()) |
| 83 | + response.anchor() |
| 84 | + self.send_prompt(event.value, response) |
| 85 | + |
| 86 | + @work(thread=True) |
| 87 | + def send_prompt(self, prompt: str, response: Response) -> None: |
| 88 | + """Get the response in a thread.""" |
| 89 | + response_content = "" |
| 90 | + llm_response = self.model.prompt(prompt, system=SYSTEM) |
| 91 | + for chunk in llm_response: |
| 92 | + response_content += chunk |
| 93 | + self.call_from_thread(response.update, response_content) |
| 94 | + |
| 95 | + |
| 96 | +if __name__ == "__main__": |
| 97 | + print( |
| 98 | + "https://textual.textualize.io/blog/2024/09/15/anatomy-of-a-textual-user-interface/" |
| 99 | + ) |
| 100 | + print( |
| 101 | + "You will need an OpenAI API key for this example.\nSee https://help.openai.com/en/articles/4936850-where-do-i-find-my-openai-api-key" |
| 102 | + ) |
| 103 | + app = MotherApp() |
| 104 | + app.run() |
0 commit comments