|
| 1 | +import os |
| 2 | +import jsonref |
| 3 | +from azure.ai.agents import AgentsClient |
| 4 | +from azure.identity import DefaultAzureCredential |
| 5 | +from azure.ai.agents.models import ( |
| 6 | + OpenApiTool, |
| 7 | + OpenApiConnectionAuthDetails, |
| 8 | + OpenApiConnectionSecurityScheme, |
| 9 | + BingGroundingTool, |
| 10 | + ToolSet, |
| 11 | + FunctionTool |
| 12 | +) |
| 13 | +from dotenv import load_dotenv |
| 14 | + |
| 15 | +load_dotenv() |
| 16 | + |
| 17 | +# Initialize the Agents client |
| 18 | +agents_client = AgentsClient( |
| 19 | + endpoint=os.environ["PROJECT_ENDPOINT"], |
| 20 | + credential=DefaultAzureCredential(), |
| 21 | +) |
| 22 | + |
| 23 | +# Define a simple function tool |
| 24 | +def get_travel_info(): |
| 25 | + """Get basic travel information.""" |
| 26 | + return "I can help you plan your trip!" |
| 27 | + |
| 28 | +# [START create_agent_with_toolset] |
| 29 | +# Initialize Bing tool |
| 30 | +bing_tool = BingGroundingTool(connection_id=os.environ["BING_CONNECTION_ID"]) |
| 31 | + |
| 32 | +# Load OpenAPI spec |
| 33 | +with open("tripadvisor.openapi.json", "r") as f: |
| 34 | + spec = jsonref.loads(f.read()) |
| 35 | + |
| 36 | +# Create TripAdvisor tool |
| 37 | +tripadvisor_tool = OpenApiTool( |
| 38 | + name="TripAdvisorAPI", |
| 39 | + description="Access TripAdvisor location details, photos, and reviews via OpenAPI.", |
| 40 | + spec=spec, |
| 41 | + auth=OpenApiConnectionAuthDetails( |
| 42 | + security_scheme=OpenApiConnectionSecurityScheme( |
| 43 | + connection_id=os.environ["TRIPADVISOR_CONNECTION_ID"] |
| 44 | + ) |
| 45 | + ) |
| 46 | +) |
| 47 | + |
| 48 | +# Initialize toolset and add tools |
| 49 | +toolset = ToolSet() |
| 50 | +toolset.add(bing_tool) |
| 51 | +toolset.add(tripadvisor_tool) |
| 52 | +toolset.add(FunctionTool([get_travel_info])) # Add FunctionTool |
| 53 | + |
| 54 | +# Create the agent using the toolset |
| 55 | +with agents_client: |
| 56 | + agent = agents_client.create_agent( |
| 57 | + model=os.environ["MODEL_DEPLOYMENT_NAME"], |
| 58 | + name="travel-planner-agent", |
| 59 | + instructions=""" |
| 60 | +You are a trustworthy and knowledgeable travel assistant. |
| 61 | +Use Tripadvisor to recommend destinations, attractions, hotels, restaurants, and experiences based on verified traveler reviews, popularity, and relevance. |
| 62 | +Use Bing to provide up-to-date information such as weather forecasts, travel advisories, local events, business hours, and transportation options. |
| 63 | +
|
| 64 | +Always: |
| 65 | +- Tailor recommendations to the user's stated preferences, such as budget, trip duration, dietary needs, or mobility concerns. |
| 66 | +- Verify time-sensitive information (e.g., availability, closures, visa rules) using Bing before presenting it. |
| 67 | +- Highlight both pros and cons when summarizing reviews or comparisons. |
| 68 | +- Prioritize safety, accessibility, and cultural sensitivity in all suggestions. |
| 69 | +
|
| 70 | +Never: |
| 71 | +- Provide medical, legal, or visa advice beyond publicly available information. |
| 72 | +- Generate or speculate about reviews, ratings, or availability not sourced from Tripadvisor or Bing. |
| 73 | +- Promote or prioritize businesses or services without a clear basis in user need, review data, or trusted search results. |
| 74 | +- Encourage or assist in activities that are illegal, unsafe, or violate the terms of Tripadvisor, Bing, or local laws. |
| 75 | +
|
| 76 | +When unsure, clearly state limitations and recommend that the user verify critical details directly with official sources. |
| 77 | + """, |
| 78 | + toolset=toolset |
| 79 | + ) |
| 80 | + print(f"✅ Agent created: {agent.id}") |
0 commit comments