|
| 1 | +from dotenv import load_dotenv |
| 2 | +load_dotenv() |
| 3 | + |
| 4 | +## - pre-reqs: install openai and azure-ai-projects packages |
| 5 | +## pip install openai azure-ai-projects azure-identity |
| 6 | +## - deploy a gpt-4o model |
| 7 | + |
| 8 | +## <chat_completion> |
| 9 | +from azure.ai.projects import AIProjectClient |
| 10 | +from azure.identity import DefaultAzureCredential |
| 11 | +from azure.ai.projects import FileSearchTool |
| 12 | + |
| 13 | +project = AIProjectClient( |
| 14 | + endpoint="https://your-foundry-resource-name.ai.azure.com/api/projects", |
| 15 | + credential=DefaultAzureCredential(), |
| 16 | +) |
| 17 | + |
| 18 | +openai = project.inference.get_azure_openai_client(api_version="2024-06-01") |
| 19 | +response = openai.chat.completions.create( |
| 20 | + model="gpt-4o", |
| 21 | + messages=[ |
| 22 | + {"role": "system", "content": "You are a helpful writing assistant"}, |
| 23 | + {"role": "user", "content": "Write me a poem about flowers"}, |
| 24 | + ], |
| 25 | +) |
| 26 | + |
| 27 | +print(response.choices[0].message.content) |
| 28 | +# </chat_completion> |
| 29 | + |
| 30 | +# <create_and_run_agent> |
| 31 | +agent = project.agents.create_agent( |
| 32 | + model="gpt-4o" |
| 33 | + name="my-agent", |
| 34 | + instructions="You are a helpful writing assistant") |
| 35 | + |
| 36 | +thread = project.agents.create_thread() |
| 37 | +message = agents_client.create_message( |
| 38 | + thread_id=thread.id, |
| 39 | + role="user", |
| 40 | + content="Write me a poem about flowers") |
| 41 | + |
| 42 | +run = project.agents.create_and_process_run(thread_id=thread.id, agent_id=agent.id) |
| 43 | +if run.status == "failed": |
| 44 | + # Check if you got "Rate limit is exceeded.", then you want to get more quota |
| 45 | + print(f"Run failed: {run.last_error}") |
| 46 | + |
| 47 | +# Get messages from the thread |
| 48 | +messages = project.agents.list_messages(thread_id=thread.id) |
| 49 | + |
| 50 | +# Get the last message from the sender |
| 51 | +last_msg = messages.get_last_text_message_by_role("assistant") |
| 52 | +if last_msg: |
| 53 | + print(f"Last Message: {last_msg.text.value}") |
| 54 | + |
| 55 | +# Delete the agent once done |
| 56 | +project.agents.delete_agent(agent.id) |
| 57 | +print("Deleted agent") |
| 58 | +# </create_and_run_agent> |
| 59 | + |
| 60 | + |
| 61 | +# <create_filesearch_agent> |
| 62 | +# Upload file and create vector store |
| 63 | +file = project.agents.upload_file(file_path="product_info_1.md", purpose="agents") |
| 64 | +vector_store = project.agents.create_vector_store_and_poll(file_ids=[file.id], name="my_vectorstore") |
| 65 | + |
| 66 | +# Create file search tool and agent |
| 67 | +file_search = FileSearchTool(vector_store_ids=[vector_store.id]) |
| 68 | +agent = project.agents.create_agent( |
| 69 | + model="gpt-4o", |
| 70 | + name="my-assistant", |
| 71 | + instructions="You are a helpful assistant and can search information from uploaded files", |
| 72 | + tools=file_search.definitions, |
| 73 | + tool_resources=file_search.resources, |
| 74 | +) |
| 75 | + |
| 76 | +# Create thread and process user message |
| 77 | +thread = project.agents.create_thread() |
| 78 | +project.agents.create_message(thread_id=thread.id, role="user", content="Hello, what Contoso products do you know?") |
| 79 | +run = project.agents.create_and_process_run(thread_id=thread.id, agent_id=agent.id) |
| 80 | + |
| 81 | +# Handle run status |
| 82 | +if run.status == "failed": |
| 83 | + print(f"Run failed: {run.last_error}") |
| 84 | + |
| 85 | +# Cleanup resources |
| 86 | +project.agents.delete_vector_store(vector_store.id) |
| 87 | +project.agents.delete_file(file_id=file.id) |
| 88 | +project.agents.delete_agent(agent.id) |
| 89 | + |
| 90 | +# Print thread messages |
| 91 | +for message in project.agents.list_messages(thread_id=thread.id).text_messages: |
| 92 | + print(message) |
| 93 | +# </create_filesearch_agent> |
| 94 | + |
| 95 | +# <evaluate_agent_run> |
| 96 | +from azure.ai.projects import EvaluatorIds |
| 97 | + |
| 98 | +result = project.evaluation.create_agent_evaluation( |
| 99 | + thread=thread.id, |
| 100 | + run=run.id, |
| 101 | + evaluators=[EvaluatorIds.AGENT_QUALITY_EVALUATOR]) |
| 102 | + |
| 103 | +# wait for evaluation to complete |
| 104 | +result.wait_for_completion() |
| 105 | + |
| 106 | +# result |
| 107 | +print(result.output()) |
| 108 | +# </evaluate_agent_run> |
0 commit comments