$git clone https://github.com/Mattie/chatsnackchatsnack is the easiest Python library for rapid development with OpenAI's ChatGPT API. It provides an intuitive interface for creating and managing chat-based prompts and responses, making it convenient to build complex, interactive conversations with AI.
| 1 | # chatsnack |
| 2 | |
| 3 | chatsnack is the easiest Python library for rapid development with OpenAI's ChatGPT API. It provides an intuitive interface for creating and managing chat-based prompts and responses, making it convenient to build complex, interactive conversations with AI. |
| 4 | |
| 5 | [Documentation site](https://mattie.github.io/chatsnack/) |
| 6 | |
| 7 |  |
| 8 | ## Setup |
| 9 | |
| 10 | ### Got snack? |
| 11 | |
| 12 | Install the `chatsnack` package from PyPI: |
| 13 | |
| 14 | ```bash |
| 15 | # requires Python 3.11+ |
| 16 | pip install chatsnack |
| 17 | ``` |
| 18 | |
| 19 | ### Got keys? |
| 20 | |
| 21 | Add your OpenAI API key to your .env file. If you don't have a .env file, the library will create a new one for you in the local directory. |
| 22 | |
| 23 | ### Learn More! |
| 24 | Read more below, watch the [intro video](https://www.youtube.com/watch?v=Yjwi54rHrhw) or check out the [Getting Started notebook](notebooks/). |
| 25 | |
| 26 | ### Responses API Support |
| 27 | `Chat()` now defaults to the Responses family over WebSocket with `session="inherit"`. |
| 28 | If you stay on that default path--or explicitly choose another Responses transport-- |
| 29 | chatsnack requires the OpenAI Python client to be `openai>=2.29.0` (or newer). |
| 30 | For explicit WebSocket support outside chatsnack's packaged dependencies, install |
| 31 | `openai[realtime]>=2.29.0`. |
| 32 | |
| 33 | ## Usage |
| 34 | |
| 35 | ### Enjoy a Quick Snack |
| 36 | |
| 37 | Easiest way to get going with `chatsnack` is with built-in snack packs. Each pack is a singleton ready to mingleton. |
| 38 | |
| 39 | ```python |
| 40 | >>> from chatsnack.packs import ChatsnackHelp |
| 41 | >>> ChatsnackHelp.ask("What is your primary directive?") |
| 42 | ``` |
| 43 | > *"My primary directive is to assist users of the chatsnack Python module by answering questions |
| 44 | and helping with any problems or concerns related to the module. I aim to provide helpful and |
| 45 | informative responses based on the chatsnack module's documentation and best practices."* |
| 46 | |
| 47 | You can try out other example snack packs like `Confectioner`, `Jolly`, `Chester`, `Jane`, or `Data`. (Eventually there will be an easy way to create and share your own.) |
| 48 | |
| 49 | Instead of `.ask()` we can call `.chat()` which will allow us to continue a conversation. |
| 50 | ```python |
| 51 | >>> mychat = ChatsnackHelp.chat("What is chatsnack?") # submits and returns a new chat object |
| 52 | >>> print(mychat.response) |
| 53 | ``` |
| 54 | > *Chatsnack is a Python module that provides a simple and powerful interface for creating conversational agents and tools using OpenAI's ChatGPT language models. It allows you to easily build chat prompts, manage conversation flow, and integrate with ChatGPT to generate responses. With Chatsnack, you can create chatbots, AI-assisted tools, and other conversational applications using a convenient and flexible API.* |
| 55 | |
| 56 | Now we can add more messages to that chat however we'd like: |
| 57 | ```python |
| 58 | >>> mychat.user("Respond in only six word sentences from now on.") |
| 59 | >>> mychat.asst("I promise I will do so.") |
| 60 | >>> mychat.user("How should I spend my day?") |
| 61 | >>> mychat.ask() |
| 62 | ``` |
| 63 | > *"Explore hobbies, exercise, connect with friends."* |
| 64 | |
| 65 | If you want a super simple interactive conversation with you and a chatbot, you could do something like this: |
| 66 | ```python |
| 67 | from chatsnack.packs import Jolly |
| 68 | yourchat = Jolly # interview a green giant |
| 69 | while (user_input := input("Chat with the bot: ")): |
| 70 | print(f"USER: {user_input}") |
| 71 | yourchat = yourchat.chat(user_input) |
| 72 | print(f"THEM: {yourchat.last}") |
| 73 | ``` |
| 74 | |
| 75 | ### Natural Attachments (Phase 3 style ergonomics) |
| 76 | |
| 77 | For the default Responses workflows, you can pass attachments directly at query time with |
| 78 | `files=` and `images=`: |
| 79 | |
| 80 | ```python |
| 81 | from chatsnack import Chat |
| 82 | |
| 83 | chat = Chat("Review the attachment and answer tersely.") |
| 84 | print(chat.ask("Summarize these.", files=["./images/chart.png", "./data/sales.csv"])) |
| 85 | |
| 86 | thread = chat.chat("What stands out in this chart?", files=["./images/chart.png"]) |
| 87 | thread = thread.chat("Now compare that with this report.", files=["./data/q2-report.pdf"]) |
| 88 | print(thread.last) |
| 89 | ``` |
| 90 | |
| 91 | When attachments are present, chatsnack stores the user turn as an expanded YAML block |
| 92 | (`text` + `files` / `i |