-
Notifications
You must be signed in to change notification settings - Fork 206
More function calling examples #12
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 2 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
d5de171
Add more function calling examples
pamelafox 8fd96bd
Function calling examples
pamelafox f70f8d9
adds new function calling examples for Spanish
madebygps 37c54c4
ran precommit
madebygps fb010a6
precommit
madebygps cae4112
Update function calling few shots example
pamelafox 02c0c36
Remove error handling from while loop example, keep it only in errors…
pamelafox f6f099c
Update parallel to show execution
pamelafox 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
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
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,170 @@ | ||
| import json | ||
| import os | ||
| from collections.abc import Callable | ||
| from typing import Any | ||
|
|
||
| import azure.identity | ||
| import openai | ||
| from dotenv import load_dotenv | ||
|
|
||
| # Setup the OpenAI client to use either Azure, OpenAI.com, or Ollama API | ||
| load_dotenv(override=True) | ||
| API_HOST = os.getenv("API_HOST", "github") | ||
|
|
||
| if API_HOST == "azure": | ||
| token_provider = azure.identity.get_bearer_token_provider( | ||
| azure.identity.DefaultAzureCredential(), "https://cognitiveservices.azure.com/.default" | ||
| ) | ||
| client = openai.OpenAI( | ||
| base_url=os.environ["AZURE_OPENAI_ENDPOINT"], | ||
| api_key=token_provider, | ||
| ) | ||
| MODEL_NAME = os.environ["AZURE_OPENAI_CHAT_DEPLOYMENT"] | ||
|
|
||
| elif API_HOST == "ollama": | ||
| client = openai.OpenAI(base_url=os.environ["OLLAMA_ENDPOINT"], api_key="nokeyneeded") | ||
| MODEL_NAME = os.environ["OLLAMA_MODEL"] | ||
|
|
||
| elif API_HOST == "github": | ||
| client = openai.OpenAI(base_url="https://models.github.ai/inference", api_key=os.environ["GITHUB_TOKEN"]) | ||
| MODEL_NAME = os.getenv("GITHUB_MODEL", "openai/gpt-4o") | ||
|
|
||
| else: | ||
| client = openai.OpenAI(api_key=os.environ["OPENAI_KEY"]) | ||
| MODEL_NAME = os.environ["OPENAI_MODEL"] | ||
|
|
||
|
|
||
| # --------------------------------------------------------------------------- | ||
| # Tool implementation(s) | ||
| # --------------------------------------------------------------------------- | ||
| def search_database(search_query: str, price_filter: dict | None = None) -> dict[str, str]: | ||
| """Search database for relevant products based on user query""" | ||
| if not search_query: | ||
| raise ValueError("search_query is required") | ||
| if price_filter: | ||
| if "comparison_operator" not in price_filter or "value" not in price_filter: | ||
| raise ValueError("Both comparison_operator and value are required in price_filter") | ||
| if price_filter["comparison_operator"] not in {">", "<", ">=", "<=", "="}: | ||
| raise ValueError("Invalid comparison_operator in price_filter") | ||
| if not isinstance(price_filter["value"], int | float): | ||
| raise ValueError("Value in price_filter must be a number") | ||
| return [{"id": "123", "name": "Example Product", "price": 19.99}] | ||
|
|
||
|
|
||
| tool_mapping: dict[str, Callable[..., Any]] = { | ||
| "search_database": search_database, | ||
| } | ||
|
|
||
| tools = [ | ||
| { | ||
| "type": "function", | ||
| "function": { | ||
| "name": "search_database", | ||
| "description": "Search database for relevant products based on user query", | ||
| "parameters": { | ||
| "type": "object", | ||
| "properties": { | ||
| "search_query": { | ||
| "type": "string", | ||
| "description": "Query string to use for full text search, e.g. 'red shoes'", | ||
| }, | ||
| "price_filter": { | ||
| "type": "object", | ||
| "description": "Filter search results based on price of the product", | ||
| "properties": { | ||
| "comparison_operator": { | ||
| "type": "string", | ||
| "description": "Operator to compare the column value, either '>', '<', '>=', '<=', '='", # noqa | ||
| }, | ||
| "value": { | ||
| "type": "number", | ||
| "description": "Value to compare against, e.g. 30", | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| "required": ["search_query"], | ||
| }, | ||
| }, | ||
| } | ||
| ] | ||
|
|
||
| messages: list[dict[str, Any]] = [ | ||
| {"role": "system", "content": "You are a product search assistant."}, | ||
| {"role": "user", "content": "Find me a red shirt under $20."}, | ||
| ] | ||
|
|
||
| print(f"Model: {MODEL_NAME} on Host: {API_HOST}\n") | ||
|
|
||
| # First model response (may include tool call) | ||
| response = client.chat.completions.create( | ||
| model=MODEL_NAME, | ||
| messages=messages, | ||
| tools=tools, | ||
| tool_choice="auto", | ||
| parallel_tool_calls=False, | ||
| ) | ||
|
|
||
| assistant_msg = response.choices[0].message | ||
|
|
||
| # If no tool calls were requested, just print the answer. | ||
| if not assistant_msg.tool_calls: | ||
| print("Assistant:") | ||
| print(assistant_msg.content) | ||
| else: | ||
| # Append assistant message including tool call metadata | ||
| messages.append( | ||
| { | ||
| "role": "assistant", | ||
| "content": assistant_msg.content or "", | ||
| "tool_calls": [tc.model_dump() for tc in assistant_msg.tool_calls], | ||
| } | ||
| ) | ||
|
|
||
| # Process each requested tool sequentially (though usually one here) | ||
| for tool_call in assistant_msg.tool_calls: | ||
| fn_name = tool_call.function.name | ||
| raw_args = tool_call.function.arguments or "{}" | ||
| print(f"Tool request: {fn_name}({raw_args})") | ||
|
|
||
| target = tool_mapping.get(fn_name) | ||
| if not target: | ||
| tool_result: Any = f"ERROR: No implementation registered for tool '{fn_name}'" | ||
| else: | ||
| # Parse arguments safely | ||
| try: | ||
| parsed_args = json.loads(raw_args) if raw_args.strip() else {} | ||
| except json.JSONDecodeError: | ||
| parsed_args = {} | ||
| tool_result = "Warning: Malformed JSON arguments received; proceeding with empty args" | ||
| else: | ||
| try: | ||
| tool_result = target(**parsed_args) | ||
| except Exception as e: # safeguard tool execution | ||
| tool_result = f"Tool execution error in {fn_name}: {e}" | ||
|
|
||
| # Serialize tool output (dict or str) as JSON string for the model | ||
| try: | ||
| tool_content = json.dumps(tool_result) | ||
| except Exception: | ||
| # Fallback to string conversion if something isn't JSON serializable | ||
| tool_content = json.dumps({"result": str(tool_result)}) | ||
|
|
||
| messages.append( | ||
| { | ||
| "role": "tool", | ||
| "tool_call_id": tool_call.id, | ||
| "name": fn_name, | ||
| "content": tool_content, | ||
| } | ||
| ) | ||
|
|
||
| # Follow-up model response after supplying tool outputs | ||
| followup = client.chat.completions.create( | ||
| model=MODEL_NAME, | ||
| messages=messages, | ||
| tools=tools, | ||
| ) | ||
| final_msg = followup.choices[0].message | ||
| print("Assistant (final):") | ||
| print(final_msg.content) |
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.
Uh oh!
There was an error while loading. Please reload this page.