|
| 1 | +""" |
| 2 | +Example showing how to use OpenAI tool calls with parameter extraction. |
| 3 | +Both synchronous and asynchronous examples are provided. |
| 4 | +
|
| 5 | +To run this example: |
| 6 | +1. Make sure you have the OpenAI API key in your .env file: |
| 7 | + OPENAI_API_KEY=your-api-key |
| 8 | +2. Run: python examples/tool_calls/openai_tool_calls.py |
| 9 | +""" |
| 10 | + |
| 11 | +import asyncio |
| 12 | +import json |
| 13 | +import os |
| 14 | +from typing import Dict, Any |
| 15 | + |
| 16 | +from dotenv import load_dotenv |
| 17 | + |
| 18 | +from neo4j_graphrag.llm import OpenAILLM |
| 19 | +from neo4j_graphrag.llm.types import ToolCallResponse |
| 20 | +from neo4j_graphrag.tool import Tool, ObjectParameter, StringParameter, IntegerParameter |
| 21 | + |
| 22 | +# Load environment variables from .env file (OPENAI_API_KEY required for this example) |
| 23 | +load_dotenv() |
| 24 | + |
| 25 | + |
| 26 | +# Create a custom Tool implementation for person info extraction |
| 27 | +parameters = ObjectParameter( |
| 28 | + description="Parameters for extracting person information", |
| 29 | + properties={ |
| 30 | + "name": StringParameter(description="The person's full name"), |
| 31 | + "age": IntegerParameter(description="The person's age"), |
| 32 | + "occupation": StringParameter(description="The person's occupation"), |
| 33 | + }, |
| 34 | + required_properties=["name"], |
| 35 | + additional_properties=False, |
| 36 | +) |
| 37 | +person_info_tool = Tool( |
| 38 | + name="extract_person_info", |
| 39 | + description="Extract information about a person from text", |
| 40 | + parameters=parameters, |
| 41 | + execute_func=lambda **kwargs: kwargs, |
| 42 | +) |
| 43 | + |
| 44 | +# Create the tool instance |
| 45 | +TOOLS = [person_info_tool] |
| 46 | + |
| 47 | + |
| 48 | +def process_tool_calls(response: ToolCallResponse) -> Dict[str, Any]: |
| 49 | + """Process all tool calls in the response and return the extracted parameters.""" |
| 50 | + if not response.tool_calls: |
| 51 | + raise ValueError("No tool calls found in response") |
| 52 | + |
| 53 | + print(f"\nNumber of tool calls: {len(response.tool_calls)}") |
| 54 | + print(f"Additional content: {response.content or 'None'}") |
| 55 | + |
| 56 | + results = [] |
| 57 | + for i, tool_call in enumerate(response.tool_calls): |
| 58 | + print(f"\nTool call #{i + 1}: {tool_call.name}") |
| 59 | + print(f"Arguments: {tool_call.arguments}") |
| 60 | + results.append(tool_call.arguments) |
| 61 | + |
| 62 | + # For backward compatibility, return the first tool call's arguments |
| 63 | + return results[0] if results else {} |
| 64 | + |
| 65 | + |
| 66 | +async def main() -> None: |
| 67 | + # Initialize the OpenAI LLM |
| 68 | + llm = OpenAILLM( |
| 69 | + api_key=os.getenv("OPENAI_API_KEY"), |
| 70 | + model_name="gpt-4o", |
| 71 | + model_params={"temperature": 0}, |
| 72 | + ) |
| 73 | + |
| 74 | + # Example text containing information about a person |
| 75 | + text = "Stella Hane is a 35-year-old software engineer who loves coding." |
| 76 | + |
| 77 | + print("\n=== Synchronous Tool Call ===") |
| 78 | + # Make a synchronous tool call |
| 79 | + sync_response = llm.invoke_with_tools( |
| 80 | + input=f"Extract information about the person from this text: {text}", |
| 81 | + tools=TOOLS, |
| 82 | + ) |
| 83 | + sync_result = process_tool_calls(sync_response) |
| 84 | + print("\n=== Synchronous Tool Call Result ===") |
| 85 | + print(json.dumps(sync_result, indent=2)) |
| 86 | + |
| 87 | + print("\n=== Asynchronous Tool Call ===") |
| 88 | + # Make an asynchronous tool call with a different text |
| 89 | + text2 = "Molly Hane, 32, works as a data scientist and enjoys machine learning." |
| 90 | + async_response = await llm.ainvoke_with_tools( |
| 91 | + input=f"Extract information about the person from this text: {text2}", |
| 92 | + tools=TOOLS, |
| 93 | + ) |
| 94 | + async_result = process_tool_calls(async_response) |
| 95 | + print("\n=== Asynchronous Tool Call Result ===") |
| 96 | + print(json.dumps(async_result, indent=2)) |
| 97 | + |
| 98 | + |
| 99 | +if __name__ == "__main__": |
| 100 | + # Run the async main function |
| 101 | + asyncio.run(main()) |
0 commit comments