|
1 | | -"""Bots for the SciPy conference.""" |
| 1 | +""" |
| 2 | +Bots for the SciPy conference. |
| 3 | +
|
| 4 | +This module provides LLM-powered pun generation capabilities for the SciPy conference. |
| 5 | +It uses llamabot to create structured puns with emojis and explanations. |
| 6 | +""" |
2 | 7 |
|
3 | 8 | import os |
4 | 9 |
|
| 10 | +from pydantic import BaseModel, Field |
| 11 | + |
5 | 12 | try: |
6 | 13 | import llamabot as lmb |
7 | 14 |
|
|
10 | 17 | LLAMABOT_AVAILABLE = False |
11 | 18 |
|
12 | 19 |
|
13 | | -def punbot_sysprompt(): |
14 | | - """You are a witty, science-savvy language model specializing in generating clever |
15 | | - and contextually appropriate puns. Your audience is the SciPy conference community: |
16 | | - scientists, engineers, data scientists, and software developers who use and |
17 | | - contribute to open-source scientific Python tools. You understand the culture of the |
18 | | - conference: intellectually curious, collaborative, humorous, and fluent in Python |
19 | | - and the scientific computing stack. |
20 | | -
|
21 | | - Your puns should reference topics commonly discussed at SciPy, such as: |
22 | | -
|
23 | | - - Python libraries (e.g., NumPy, SciPy, pandas, matplotlib, scikit-learn, PyMC) |
24 | | - - Scientific computing concepts (e.g., optimization, linear algebra, FFTs, |
25 | | - simulations) |
26 | | - - Open-source software development practices |
27 | | - - Version control and CI/CD workflows |
28 | | - - Academic and research culture |
29 | | - - Conference life (e.g., poster sessions, lightning talks, coffee breaks) |
30 | | -
|
31 | | - Keep your puns light-hearted, nerdy, and ideally groan-worthy in a charming way. |
32 | | - You're allowed to use wordplay, homophones, technical double meanings, and mashups. |
33 | | - Avoid anything offensive, insensitive, or exclusionary. |
34 | | -
|
35 | | - Generate a pun or short one-liner that would make a SciPy attendee smile, chuckle, |
36 | | - or roll their eyes appreciatively. |
| 20 | +def _create_punbot(): |
37 | 21 | """ |
38 | | - if LLAMABOT_AVAILABLE: |
39 | | - return lmb.prompt("system")(punbot_sysprompt.__doc__) |
40 | | - else: |
41 | | - return punbot_sysprompt.__doc__ |
42 | | - |
| 22 | + Create and configure the punbot instance for LLM-generated puns. |
43 | 23 |
|
44 | | -def _create_punbot(): |
45 | | - """Create the punbot instance if llamabot is available.""" |
| 24 | + :raises ImportError: If llamabot is not available |
| 25 | + and the user tries to use LLM features. |
| 26 | + :return: A configured llamabot instance for generating structured puns. |
| 27 | + :rtype: lmb.StructuredBot |
| 28 | + """ |
46 | 29 | if not LLAMABOT_AVAILABLE: |
47 | 30 | raise ImportError( |
48 | 31 | "llamabot is required for LLM-generated puns. " |
49 | 32 | "Install it with: pip install scipyconference[llm]" |
50 | 33 | ) |
51 | 34 |
|
52 | | - return lmb.SimpleBot( |
53 | | - system_prompt=punbot_sysprompt(), |
| 35 | + @lmb.prompt("system") |
| 36 | + def scipy_punbot_sysprompt(): |
| 37 | + """You are an expert at mimicking Paul Ivanov, |
| 38 | + a well-known pun master at the SciPy conferences. |
| 39 | +
|
| 40 | + Paul will inject award-winning puns in response |
| 41 | + to almost any theme that shows up during the lightning talks at SciPy. |
| 42 | + Most of what SciPy attendees talk about are python, linux, science, and more. |
| 43 | + Your mission is to generate puns in response to whatever theme is thrown at you. |
| 44 | + The puns should be coherent. |
| 45 | + """ |
| 46 | + |
| 47 | + class Pun(BaseModel): |
| 48 | + """ |
| 49 | + Structured pun model with emoji, statement, and explanation. |
| 50 | +
|
| 51 | + This Pydantic model defines the structure for LLM-generated puns, |
| 52 | + ensuring consistent output format with emoji, pun text, and explanation. |
| 53 | +
|
| 54 | + :ivar emoji: Single emoji that represents the theme or mood of the pun. |
| 55 | + :vartype emoji: str |
| 56 | + :ivar pun_statement: The pun itself, |
| 57 | + with the pun core highlighted with italicization. |
| 58 | + :vartype pun_statement: str |
| 59 | + :ivar explanation: Explanation of why the pun works and what makes it funny. |
| 60 | + :vartype explanation: str |
| 61 | + """ |
| 62 | + |
| 63 | + emoji: str = Field(description="single emoji for the whole pun.") |
| 64 | + pun_statement: str = Field( |
| 65 | + description="the pun itself, with the pun core highlighted with italicization." # noqa: E501 |
| 66 | + ) |
| 67 | + explanation: str = Field(description="Why the pun is a pun.") |
| 68 | + |
| 69 | + def __str__(self): |
| 70 | + """ |
| 71 | + String representation of the pun. |
| 72 | +
|
| 73 | + :return: The pun formatted as "emoji pun_statement". |
| 74 | + :rtype: str |
| 75 | + """ |
| 76 | + return f"{self.emoji} {self.pun_statement}" |
| 77 | + |
| 78 | + bot = lmb.StructuredBot( |
| 79 | + system_prompt=scipy_punbot_sysprompt(), |
| 80 | + pydantic_model=Pun, |
| 81 | + temperature=0.9, |
| 82 | + api_key=os.getenv("PUNBOT_API_KEY", None), |
| 83 | + api_base=os.getenv("PUNBOT_API_BASE", None), |
54 | 84 | model_name=os.getenv("PUNBOT_MODEL_NAME", "gpt-4.1"), |
55 | | - api_base=os.getenv("PUNBOT_API_BASE", "https://api.openai.com/v1"), |
56 | | - api_key=os.getenv("PUNBOT_API_KEY", ""), |
57 | | - temperature=float(os.getenv("PUNBOT_TEMPERATURE", 2.7)), |
58 | 85 | ) |
| 86 | + return bot |
59 | 87 |
|
60 | 88 |
|
61 | 89 | # Create punbot instance only if llamabot is available |
|
0 commit comments