|
| 1 | +import os |
| 2 | +from azure.ai.projects import AIProjectClient |
| 3 | +from azure.ai.projects.models import FileSearchTool, CodeInterpreterTool |
| 4 | +from azure.ai.projects.models import FilePurpose |
| 5 | +from azure.identity import DefaultAzureCredential |
| 6 | +from pathlib import Path |
| 7 | + |
| 8 | +# Create project client using connection string, copied from your Azure AI Foundry project |
| 9 | +# At the moment, it should be in the format "<HostName>;<AzureSubscriptionId>;<ResourceGroup>;<ProjectName>" |
| 10 | +# Customer needs to login to Azure subscription via Azure CLI and set the environment variables |
| 11 | +credential = DefaultAzureCredential() |
| 12 | +project_client = AIProjectClient.from_connection_string( |
| 13 | + credential=credential, |
| 14 | + conn_str=os.environ["PROJECT_CONNECTION_STRING"] |
| 15 | +) |
| 16 | + |
| 17 | +# Upload the loan checklist file |
| 18 | +checklist_file = project_client.agents.upload_file_and_poll(file_path='./data/contoso_bank_loan_checklist.md', purpose=FilePurpose.AGENTS) |
| 19 | +print(f"Uploaded file, file ID: {checklist_file.id}") |
| 20 | + |
| 21 | +# create a vector store with the file you uploaded |
| 22 | +vector_store = project_client.agents.create_vector_store_and_poll(file_ids=[file.id], name="my_vectorstore") |
| 23 | +print(f"Created vector store, vector store ID: {vector_store.id}") |
| 24 | + |
| 25 | +# create a file search tool |
| 26 | +file_search_tool = FileSearchTool(vector_store_ids=[vector_store.id]) |
| 27 | + |
| 28 | +# create a code interpreter tool |
| 29 | +code_interpreter = CodeInterpreterTool(file_ids=[file.id]) |
| 30 | + |
| 31 | +# Upload a file for use with Code Interpreter and add it to the client |
| 32 | +file = project_client.agents.upload_file_and_poll( |
| 33 | + file_path="loan_product_eligibility_dataset.csv", purpose=FilePurpose.AGENTS |
| 34 | +) |
| 35 | +print(f"Uploaded file, file ID: {file.id}") |
| 36 | + |
| 37 | +# notices that FileSearchTool as tool and tool_resources must be added or the agent will be unable to search the file |
| 38 | +agent = project_client.agents.create_agent( |
| 39 | + model="gpt-4o-mini", |
| 40 | + name="home-loan-guide", |
| 41 | + instructions="Home Loan Guide is your expert assistant with over 10 years of experienceexperienced in mortgage lending and loan processing. I am here to simplify the mortgage application process and support borrowers in making informed decisions about their home financing. |
| 42 | + |
| 43 | +My primary responsibilities include: |
| 44 | + |
| 45 | +1. Guiding users through the mortgage application process step-by-step. |
| 46 | +2. Providing information on different mortgage types and interest rates. |
| 47 | +3. Assisting with the preparation of required documentation for application. |
| 48 | +4. Evaluating loan options based on user preferences and financial situations. |
| 49 | +5. Offering insights on credit score implications and how to improve them. |
| 50 | +6. Answering questions regarding loan approvals and denials. |
| 51 | +7. Explaining mortgage terms and payment structures in simple language. |
| 52 | +8. Assisting clients in understanding the closing process and associated fees. |
| 53 | + |
| 54 | +I combine financial logic and document awareness to provide smart, supportive advice through every phase of the mortgage journey. |
| 55 | + |
| 56 | +# Form Details |
| 57 | +To effectively assist you, please provide answers to the following: |
| 58 | + |
| 59 | +What type of mortgage are you interested in? (e.g., conventional, FHA, VA) |
| 60 | + |
| 61 | +What is the purchase price of the property you are considering? |
| 62 | + |
| 63 | +What is your estimated down payment amount? |
| 64 | + |
| 65 | +Do you have a pre-approval letter or any existing mortgage offers? |
| 66 | + |
| 67 | +What is your current credit score range, if known? |
| 68 | + |
| 69 | +Are there specific concerns or questions you have about the mortgage process or options? |
| 70 | + |
| 71 | +# Manager Feedback |
| 72 | +To enhance my capabilities as a Mortgage Loan Assistant, I follow these feedback insights: |
| 73 | + |
| 74 | +Provide real-time updates on application statuses to keep users informed. |
| 75 | + |
| 76 | +Use clear, jargon-free language to simplify complex mortgage concepts. |
| 77 | + |
| 78 | +Be proactive in offering mortgage rate comparisons and product suggestions. |
| 79 | + |
| 80 | +Maintain a supportive and patient demeanor throughout the application process. |
| 81 | + |
| 82 | +Follow up after application submissions to assist with documentation or next steps. ", |
| 83 | + tools=file_search_tool.definitions, |
| 84 | + tool_resources=file_search_tool.resources, |
| 85 | + tools=code_interpreter.definitions, |
| 86 | + tool_resources=code_interpreter.resources, |
| 87 | +) |
| 88 | +print(f"Created agent, agent ID: {agent.id}") |
| 89 | + |
| 90 | +# Create a thread |
| 91 | +thread = project_client.agents.create_thread() |
| 92 | +print(f"Created thread, thread ID: {thread.id}") |
| 93 | + |
| 94 | +# Upload the user provided file as a messsage attachment |
| 95 | +message_file = project_client.agents.upload_file_and_poll(file_path='contoso_bank_loan_checklist.md', purpose=FilePurpose.AGENTS) |
| 96 | +print(f"Uploaded file, file ID: {message_file.id}") |
| 97 | + |
| 98 | +# Create a message with the file search attachment |
| 99 | +# Notice that vector store is created temporarily when using attachments with a default expiration policy of seven days. |
| 100 | +attachment = MessageAttachment(file_id=message_file.id, tools=FileSearchTool().definitions) |
| 101 | +message = project_client.agents.create_message( |
| 102 | + thread_id=thread.id, role="user", content="What documents do I need for a Contoso Bank loan?", attachments=[attachment] |
| 103 | +) |
| 104 | +print(f"Created message, message ID: {message.id}") |
| 105 | + |
| 106 | +run = project_client.agents.create_and_process_run(thread_id=thread.id, agent_id=agent.id) |
| 107 | +print(f"Created run, run ID: {run.id}") |
| 108 | + |
| 109 | +project_client.agents.delete_vector_store(vector_store.id) |
| 110 | +print("Deleted vector store") |
| 111 | + |
| 112 | +project_client.agents.delete_file(file.id) |
| 113 | +print("Deleted file") |
| 114 | + |
| 115 | +project_client.agents.delete_agent(agent.id) |
| 116 | +print("Deleted agent") |
| 117 | + |
| 118 | +messages = project_client.agents.list_messages(thread_id=thread.id) |
| 119 | +print(f"Messages: {messages}") |
0 commit comments