generated from slack-samples/bolt-python-starter-template
-
Notifications
You must be signed in to change notification settings - Fork 12
feat: showcase text generation and thinking steps from suggested prompts #37
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
242297a
feat: showcase text generation and thinking steps from suggested prompts
zimeg ea6c722
fix: append to details instead of overwrite past arguments
zimeg 4a5de84
feat: roll dice
zimeg ac2816f
docs: improve comments to reason about sections of code
zimeg 6ab8795
refactor: move 'ai' features to an 'agent' directory
zimeg e2f6fa2
refactor: reverse the order of expected prompts for longform demo first
zimeg 7ddcac0
fix: match verbe tenses of actions and details in steps
zimeg d6af288
docs: correct spellings of shocked wordage in example
zimeg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,101 @@ | ||
| import json | ||
| import os | ||
|
|
||
| import openai | ||
| from openai.types.responses import ResponseInputParam | ||
| from slack_sdk.models.messages.chunk import TaskUpdateChunk | ||
| from slack_sdk.web.chat_stream import ChatStream | ||
|
|
||
| from agent.tools.dice import roll_dice, roll_dice_definition | ||
|
|
||
|
|
||
| def call_llm( | ||
| streamer: ChatStream, | ||
| prompts: ResponseInputParam, | ||
| ): | ||
| """ | ||
| Stream an LLM response to prompts with an example dice rolling function | ||
|
|
||
| https://docs.slack.dev/tools/python-slack-sdk/web#sending-streaming-messages | ||
| https://platform.openai.com/docs/guides/text | ||
| https://platform.openai.com/docs/guides/streaming-responses | ||
| https://platform.openai.com/docs/guides/function-calling | ||
| """ | ||
| llm = openai.OpenAI( | ||
| api_key=os.getenv("OPENAI_API_KEY"), | ||
| ) | ||
| tool_calls = [] | ||
| response = llm.responses.create( | ||
| model="gpt-4o-mini", | ||
| input=prompts, | ||
| tools=[ | ||
| roll_dice_definition, | ||
| ], | ||
| stream=True, | ||
| ) | ||
| for event in response: | ||
| # Markdown text from the LLM response is streamed in chat as it arrives | ||
| if event.type == "response.output_text.delta": | ||
| streamer.append(markdown_text=f"{event.delta}") | ||
|
|
||
| # Function calls are saved for later computation and a new task is shown | ||
| if event.type == "response.output_item.done": | ||
| if event.item.type == "function_call": | ||
| tool_calls.append(event.item) | ||
| if event.item.name == "roll_dice": | ||
| args = json.loads(event.item.arguments) | ||
| streamer.append( | ||
| chunks=[ | ||
| TaskUpdateChunk( | ||
| id=f"{event.item.call_id}", | ||
| title=f"Rolling a {args['count']}d{args['sides']}...", | ||
| status="in_progress", | ||
| ), | ||
| ], | ||
| ) | ||
|
|
||
| # Tool calls are performed and tasks are marked as completed in Slack | ||
| if tool_calls: | ||
| for call in tool_calls: | ||
| if call.name == "roll_dice": | ||
| args = json.loads(call.arguments) | ||
| prompts.append( | ||
| { | ||
| "id": call.id, | ||
| "call_id": call.call_id, | ||
| "type": "function_call", | ||
| "name": "roll_dice", | ||
| "arguments": call.arguments, | ||
| } | ||
| ) | ||
| result = roll_dice(**args) | ||
| prompts.append( | ||
| { | ||
| "type": "function_call_output", | ||
| "call_id": call.call_id, | ||
| "output": json.dumps(result), | ||
| } | ||
| ) | ||
| if result.get("error") is not None: | ||
| streamer.append( | ||
| chunks=[ | ||
| TaskUpdateChunk( | ||
| id=f"{call.call_id}", | ||
| title=f"{result['error']}", | ||
| status="error", | ||
| ), | ||
| ], | ||
| ) | ||
| else: | ||
| streamer.append( | ||
| chunks=[ | ||
| TaskUpdateChunk( | ||
| id=f"{call.call_id}", | ||
| title=f"{result['description']}", | ||
| status="complete", | ||
| ), | ||
| ], | ||
| ) | ||
|
|
||
| # Complete the LLM response after making tool calls | ||
| call_llm(streamer, prompts) |
Empty file.
This file contains hidden or 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,60 @@ | ||
| import random | ||
| import time | ||
|
|
||
| from openai.types.responses import FunctionToolParam | ||
|
|
||
|
|
||
| def roll_dice(sides: int = 6, count: int = 1) -> dict: | ||
| if sides < 2: | ||
| return { | ||
| "error": "A die must have at least 2 sides", | ||
| "rolls": [], | ||
| "total": 0, | ||
| } | ||
|
|
||
| if count < 1: | ||
| return { | ||
| "error": "Must roll at least 1 die", | ||
| "rolls": [], | ||
| "total": 0, | ||
| } | ||
|
|
||
| # Roll the dice and calculate the total | ||
| rolls = [random.randint(1, sides) for _ in range(count)] | ||
| total = sum(rolls) | ||
|
|
||
| # Add a pause between rolls to demonstrate loading states | ||
| time.sleep(2) | ||
|
|
||
| return { | ||
| "rolls": rolls, | ||
| "total": total, | ||
| "description": f"Rolled a {count}d{sides} to total {total}", | ||
| } | ||
|
|
||
|
|
||
| # Tool definition for OpenAI API | ||
| # | ||
| # https://platform.openai.com/docs/guides/function-calling | ||
| roll_dice_definition: FunctionToolParam = { | ||
| "type": "function", | ||
| "name": "roll_dice", | ||
| "description": "Roll one or more dice with a specified number of sides. Use this when the user wants to roll dice or generate random numbers within a range.", | ||
| "parameters": { | ||
| "type": "object", | ||
| "properties": { | ||
| "sides": { | ||
| "type": "integer", | ||
| "description": "The number of sides on the die (e.g., 6 for a standard die, 20 for a d20)", | ||
| "default": 6, | ||
| }, | ||
| "count": { | ||
| "type": "integer", | ||
| "description": "The number of dice to roll", | ||
| "default": 1, | ||
| }, | ||
| }, | ||
| "required": ["sides", "count"], | ||
| }, | ||
| "strict": False, | ||
| } |
This file was deleted.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i really like this example 🤩
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@srtaalej It makes for fun games! 🎲 ✨