|
| 1 | +import json |
| 2 | +import os |
| 3 | +from collections.abc import Callable |
| 4 | +from typing import Any |
| 5 | + |
| 6 | +import azure.identity |
| 7 | +import openai |
| 8 | +from dotenv import load_dotenv |
| 9 | + |
| 10 | +# Setup the OpenAI client to use either Azure, OpenAI.com, or Ollama API |
| 11 | +load_dotenv(override=True) |
| 12 | +API_HOST = os.getenv("API_HOST", "github") |
| 13 | + |
| 14 | +if API_HOST == "azure": |
| 15 | + token_provider = azure.identity.get_bearer_token_provider( |
| 16 | + azure.identity.DefaultAzureCredential(), "https://cognitiveservices.azure.com/.default" |
| 17 | + ) |
| 18 | + client = openai.OpenAI( |
| 19 | + base_url=os.environ["AZURE_OPENAI_ENDPOINT"], |
| 20 | + api_key=token_provider, |
| 21 | + ) |
| 22 | + MODEL_NAME = os.environ["AZURE_OPENAI_CHAT_DEPLOYMENT"] |
| 23 | + |
| 24 | +elif API_HOST == "ollama": |
| 25 | + client = openai.OpenAI(base_url=os.environ["OLLAMA_ENDPOINT"], api_key="nokeyneeded") |
| 26 | + MODEL_NAME = os.environ["OLLAMA_MODEL"] |
| 27 | + |
| 28 | +elif API_HOST == "github": |
| 29 | + client = openai.OpenAI(base_url="https://models.github.ai/inference", api_key=os.environ["GITHUB_TOKEN"]) |
| 30 | + MODEL_NAME = os.getenv("GITHUB_MODEL", "openai/gpt-4o") |
| 31 | + |
| 32 | +else: |
| 33 | + client = openai.OpenAI(api_key=os.environ["OPENAI_KEY"]) |
| 34 | + MODEL_NAME = os.environ["OPENAI_MODEL"] |
| 35 | + |
| 36 | + |
| 37 | +# --------------------------------------------------------------------------- |
| 38 | +# Tool implementation(s) |
| 39 | +# --------------------------------------------------------------------------- |
| 40 | +def search_database(search_query: str, price_filter: dict | None = None) -> dict[str, str]: |
| 41 | + """Search database for relevant products based on user query""" |
| 42 | + if not search_query: |
| 43 | + raise ValueError("search_query is required") |
| 44 | + if price_filter: |
| 45 | + if "comparison_operator" not in price_filter or "value" not in price_filter: |
| 46 | + raise ValueError("Both comparison_operator and value are required in price_filter") |
| 47 | + if price_filter["comparison_operator"] not in {">", "<", ">=", "<=", "="}: |
| 48 | + raise ValueError("Invalid comparison_operator in price_filter") |
| 49 | + if not isinstance(price_filter["value"], int | float): |
| 50 | + raise ValueError("Value in price_filter must be a number") |
| 51 | + return [{"id": "123", "name": "Example Product", "price": 19.99}] |
| 52 | + |
| 53 | + |
| 54 | +tool_mapping: dict[str, Callable[..., Any]] = { |
| 55 | + "search_database": search_database, |
| 56 | +} |
| 57 | + |
| 58 | +tools = [ |
| 59 | + { |
| 60 | + "type": "function", |
| 61 | + "function": { |
| 62 | + "name": "search_database", |
| 63 | + "description": "Search database for relevant products based on user query", |
| 64 | + "parameters": { |
| 65 | + "type": "object", |
| 66 | + "properties": { |
| 67 | + "search_query": { |
| 68 | + "type": "string", |
| 69 | + "description": "Query string to use for full text search, e.g. 'red shoes'", |
| 70 | + }, |
| 71 | + "price_filter": { |
| 72 | + "type": "object", |
| 73 | + "description": "Filter search results based on price of the product", |
| 74 | + "properties": { |
| 75 | + "comparison_operator": { |
| 76 | + "type": "string", |
| 77 | + "description": "Operator to compare the column value, either '>', '<', '>=', '<=', '='", # noqa |
| 78 | + }, |
| 79 | + "value": { |
| 80 | + "type": "number", |
| 81 | + "description": "Value to compare against, e.g. 30", |
| 82 | + }, |
| 83 | + }, |
| 84 | + }, |
| 85 | + }, |
| 86 | + "required": ["search_query"], |
| 87 | + }, |
| 88 | + }, |
| 89 | + } |
| 90 | +] |
| 91 | + |
| 92 | +messages: list[dict[str, Any]] = [ |
| 93 | + {"role": "system", "content": "You are a product search assistant."}, |
| 94 | + {"role": "user", "content": "Find me a red shirt under $20."}, |
| 95 | +] |
| 96 | + |
| 97 | +print(f"Model: {MODEL_NAME} on Host: {API_HOST}\n") |
| 98 | + |
| 99 | +# First model response (may include tool call) |
| 100 | +response = client.chat.completions.create( |
| 101 | + model=MODEL_NAME, |
| 102 | + messages=messages, |
| 103 | + tools=tools, |
| 104 | + tool_choice="auto", |
| 105 | + parallel_tool_calls=False, |
| 106 | +) |
| 107 | + |
| 108 | +assistant_msg = response.choices[0].message |
| 109 | + |
| 110 | +# If no tool calls were requested, just print the answer. |
| 111 | +if not assistant_msg.tool_calls: |
| 112 | + print("Assistant:") |
| 113 | + print(assistant_msg.content) |
| 114 | +else: |
| 115 | + # Append assistant message including tool call metadata |
| 116 | + messages.append( |
| 117 | + { |
| 118 | + "role": "assistant", |
| 119 | + "content": assistant_msg.content or "", |
| 120 | + "tool_calls": [tc.model_dump() for tc in assistant_msg.tool_calls], |
| 121 | + } |
| 122 | + ) |
| 123 | + |
| 124 | + # Process each requested tool sequentially (though usually one here) |
| 125 | + for tool_call in assistant_msg.tool_calls: |
| 126 | + fn_name = tool_call.function.name |
| 127 | + raw_args = tool_call.function.arguments or "{}" |
| 128 | + print(f"Tool request: {fn_name}({raw_args})") |
| 129 | + |
| 130 | + target = tool_mapping.get(fn_name) |
| 131 | + if not target: |
| 132 | + tool_result: Any = f"ERROR: No implementation registered for tool '{fn_name}'" |
| 133 | + else: |
| 134 | + # Parse arguments safely |
| 135 | + try: |
| 136 | + parsed_args = json.loads(raw_args) if raw_args.strip() else {} |
| 137 | + except json.JSONDecodeError: |
| 138 | + parsed_args = {} |
| 139 | + tool_result = "Warning: Malformed JSON arguments received; proceeding with empty args" |
| 140 | + else: |
| 141 | + try: |
| 142 | + tool_result = target(**parsed_args) |
| 143 | + except Exception as e: # safeguard tool execution |
| 144 | + tool_result = f"Tool execution error in {fn_name}: {e}" |
| 145 | + |
| 146 | + # Serialize tool output (dict or str) as JSON string for the model |
| 147 | + try: |
| 148 | + tool_content = json.dumps(tool_result) |
| 149 | + except Exception: |
| 150 | + # Fallback to string conversion if something isn't JSON serializable |
| 151 | + tool_content = json.dumps({"result": str(tool_result)}) |
| 152 | + |
| 153 | + messages.append( |
| 154 | + { |
| 155 | + "role": "tool", |
| 156 | + "tool_call_id": tool_call.id, |
| 157 | + "name": fn_name, |
| 158 | + "content": tool_content, |
| 159 | + } |
| 160 | + ) |
| 161 | + |
| 162 | + # Follow-up model response after supplying tool outputs |
| 163 | + followup = client.chat.completions.create( |
| 164 | + model=MODEL_NAME, |
| 165 | + messages=messages, |
| 166 | + tools=tools, |
| 167 | + ) |
| 168 | + final_msg = followup.choices[0].message |
| 169 | + print("Assistant (final):") |
| 170 | + print(final_msg.content) |
0 commit comments