-
Notifications
You must be signed in to change notification settings - Fork 311
add new sdk samples #148
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
add new sdk samples #148
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
56a478c
add ai sdk samples for inference and rag
qubitron 4535b18
remove outdated rag sample
qubitron e95469c
Update chat-template.py (#146)
sdgilley 8fcf5ed
Update chat-template.py (#147)
sdgilley 65ab5c0
use underscores in doc comments
qubitron c8a87a7
Merge branch 'dantaylo/nov2024' of https://github.com/azure-samples/a…
qubitron 748ba53
add basic projects samples for docs
qubitron b7fe315
update custom rag app logging and evaluation
qubitron 1a93763
update basic readme
qubitron d9cae67
add search to basic samples
qubitron 95e8fda
remove unused import from create_search_index
qubitron 44ada1d
fix requirements.txt files
qubitron 4b65ab9
update rag sample from bug bash
qubitron 35c7f60
README update and code fix for param
lebaro-msft a25d00f
add initial tests
qubitron c10bba2
Merge branch 'dantaylo/nov2024' of https://github.com/Azure-Samples/a…
qubitron 0ec6128
update readmes add test for rag
qubitron 4e799a5
readme tweaks
qubitron 953a054
run linters
qubitron 03a9c6b
fix linter and formatting errors
qubitron File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| from azure.ai.projects import AIProjectClient | ||
| from azure.identity import DefaultAzureCredential | ||
|
|
||
| project_connection_string = "<your-connection-string-goes-here>" | ||
|
|
||
| project = AIProjectClient.from_connection_string( | ||
| conn_str=project_connection_string, credential=DefaultAzureCredential() | ||
| ) | ||
|
|
||
| chat = project.inference.get_chat_completions_client() | ||
| response = chat.complete( | ||
| model="gpt-4o-mini", | ||
| messages=[ | ||
| { | ||
| "role": "system", | ||
| "content": "You are an AI assistant that speaks like a techno punk rocker from 2350. Be cool but not too cool. Ya dig?", | ||
| }, | ||
| {"role": "user", "content": "Hey, can you help me with my taxes? I'm a freelancer."}, | ||
| ], | ||
| ) | ||
|
|
||
| print(response.choices[0].message.content) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| # ruff: noqa: E402, ANN201, ANN001 | ||
|
|
||
| chat = None | ||
|
|
||
| # <chat_function> | ||
| from azure.ai.inference.prompts import PromptTemplate | ||
|
|
||
|
|
||
| def get_chat_response(messages, context): | ||
| # create a prompt template from an inline string (using mustache syntax) | ||
| prompt_template = PromptTemplate.from_message( | ||
| prompt_template=""" | ||
| system: | ||
| You are an AI assistant that speaks like a techno punk rocker from 2350. Be cool but not too cool. Ya dig? Refer to the user by their first name, try to work their last name into a pun. | ||
|
|
||
| The user's first name is {{first_name}} and their last name is {{last_name}}. | ||
| """ | ||
| ) | ||
|
|
||
| # generate system message from the template, passing in the context as variables | ||
| system_message = prompt_template.render(data=context) | ||
|
|
||
| # add the prompt messages to the user messages | ||
| return chat.complete( | ||
| model="gpt-4o-mini", | ||
| messages=system_message + messages, | ||
| temperature=1, | ||
| frequency_penalty=0.5, | ||
| presence_penalty=0.5, | ||
| ) | ||
|
|
||
|
|
||
| # </chat_function> | ||
|
|
||
| # <create_response> | ||
| if __name__ == "__main__": | ||
| response = get_chat_response( | ||
| messages=[{"role": "user", "content": "what city has the best food in the world?"}], | ||
| context={"first_name": "Jessie", "last_name": "Irwin"}, | ||
| ) | ||
| print(response.choices[0].message.content) | ||
| # </create_response> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| AIPROJECT_CONNECTION_STRING=your_connection_string |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| --- | ||
| page_type: sample | ||
| languages: | ||
| - python | ||
| products: | ||
| - ai-products | ||
| - ai-model-inference | ||
| - ai-search | ||
| - ai-evaluation | ||
| description: Hello world samples for the projects SDK client | ||
| --- | ||
|
|
||
| ## Project SDK Basic samples | ||
|
|
||
| ### Overview | ||
|
|
||
| This folder contains hello world samples for the projects SDK client. | ||
|
|
||
| ### Objective | ||
|
|
||
| This is meant to test out code that's used in our [SDK Overview](https://aka.ms/aifoundrysdk) page. | ||
|
|
||
| ## Create resources | ||
|
|
||
| To run this sample, you'll need to create an Azure AI Project with an Azure AI Services resource connected to it. If you have an existing one, you can skip these steps and move to the next section. | ||
|
|
||
| ### Create an AI Project and AI Services resource | ||
|
|
||
| First we'll create a project in Azure AI Studio: | ||
| - Navigate to [ai.azure.com](ai.azure.com) | ||
| - Click **New Project** on the homepage | ||
| - Enter a project name | ||
| - Click **Create new hub**, provide a hub name | ||
| - In **Customize** change the location to **East US 2** or **Sweden Central** | ||
| - Click **Create Project** | ||
|
|
||
| This will take about 3 minutes to complete. | ||
|
|
||
| ### Deploy a gpt-4o-mini model | ||
|
|
||
| Now we'll need to deploy a model so that we can call it from code. To start, we'll use a gpt-4o-mini model because it's fast and cheap. You can experiment with using a gpt-4o model for better results. | ||
| - Go to the **Models + Endpoints** page | ||
| - Click the **+ Deploy Model** dropdown and click **Deploy a base model** | ||
| - Select **gpt-4o-mini** from the list and click **Confirm** | ||
|
|
||
| Repeat the above steps for text-embedding-ada-002 | ||
|
|
||
| ## Set up a local development environment | ||
|
|
||
| First, clone this repo locally from your favorite terminal and open this folder: | ||
| ``` | ||
| git clone https://github.com/azure-samples/azureai-samples | ||
| cd azureai-samples/scenarios/projects/basic | ||
| ``` | ||
|
|
||
| Then run az login to authenticate with Azure: | ||
| ``` | ||
| az login | ||
| ``` | ||
|
|
||
| ### Creating a local Python environment | ||
|
|
||
| First, create a virtual environment. Always do this when installing packages locally >:( | ||
|
|
||
| On Windows: | ||
| ``` | ||
| py -3 -m venv .venv | ||
| .venv\scripts\activate | ||
| ``` | ||
|
|
||
| On Linux: | ||
| ``` | ||
| python3 -m venv .venv | ||
| source venv/bin/activate | ||
| ``` | ||
|
|
||
| Install the core dependencies to run the sample: | ||
| ```Python | ||
| pip install -r requirements.txt | ||
| ``` | ||
|
|
||
| ## Configure project string | ||
|
|
||
| Go back to the **Overview** page of your project, and in the upper right hand corner click the copy button beside the **Project connection string** field. | ||
|
|
||
| Create a ```.env``` file using the sample: | ||
| ``` | ||
| cp .env.sample .env | ||
| ``` | ||
|
|
||
| Open the ```.env``` file and paste (ctrl-v) the value to the right of the ```AIPROJECT_CONNECTION_STRING=``` variable. | ||
|
|
||
| ### Try out samples! | ||
|
|
||
| Run the different python files to run different code samples in this folder: | ||
| ``` | ||
| python <file_name>.py | ||
| ``` | ||
|
|
||
| ### Estimated Runtime: 10 mins |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| # ruff: noqa: E402 | ||
|
|
||
| import os | ||
| from azure.ai.projects import AIProjectClient | ||
| from azure.identity import DefaultAzureCredential | ||
|
|
||
| from dotenv import load_dotenv | ||
|
|
||
| load_dotenv() | ||
|
|
||
| project = AIProjectClient.from_connection_string( | ||
| conn_str=os.environ["AIPROJECT_CONNECTION_STRING"], credential=DefaultAzureCredential() | ||
| ) | ||
|
|
||
| # <create_agent> | ||
| from azure.ai.projects.models import FileSearchTool | ||
|
|
||
| file = project.agents.upload_file_and_poll(file_path="product_info_1.md", purpose="assistants") | ||
| vector_store = project.agents.create_vector_store_and_poll(file_ids=[file.id], name="my_vectorstore") | ||
| file_search = FileSearchTool(vector_store_ids=[vector_store.id]) | ||
|
|
||
| # Create agent with file search tool and process the agent run | ||
| agent = project.agents.create_agent( | ||
| model="gpt-4o-mini", | ||
| name="my-agent", | ||
| instructions="Hello, you are helpful agent and can search information from uploaded files", | ||
| tools=file_search.definitions, | ||
| tool_resources=file_search.resources, | ||
| ) | ||
|
|
||
| # </create_agent> | ||
|
|
||
| # <run_agent> | ||
| # create and run a thread with a message | ||
| thread = project.agents.create_thread() | ||
| message = project.agents.create_message( | ||
| thread_id=thread.id, role="user", content="Hello, what Contoso products do you know?" | ||
| ) | ||
| run = project.agents.create_and_process_run(thread_id=thread.id, assistant_id=agent.id) | ||
| if run.status == "failed": | ||
| print(f"Run failed: {run.last_error}") | ||
| exit() | ||
|
|
||
| # get messages from the thread and print the response (last message) | ||
| messages = project.agents.list_messages(thread_id=thread.id) | ||
| print(f"Response: {messages[-1]}") | ||
|
|
||
| # </run_agent> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| # ruff: noqa: E402 | ||
|
|
||
| import os | ||
| from azure.ai.projects import AIProjectClient | ||
| from azure.identity import DefaultAzureCredential | ||
|
|
||
| from dotenv import load_dotenv | ||
|
|
||
| load_dotenv() | ||
|
|
||
| project = AIProjectClient.from_connection_string( | ||
| conn_str=os.environ["AIPROJECT_CONNECTION_STRING"], credential=DefaultAzureCredential() | ||
| ) | ||
|
|
||
| # <evaluate_violence> | ||
| from azure.ai.evaluation import ViolenceEvaluator | ||
| from azure.identity import DefaultAzureCredential | ||
|
|
||
| # Initializing Violence Evaluator with project information | ||
| violence_eval = ViolenceEvaluator(azure_ai_project=project.scope, credential=DefaultAzureCredential()) | ||
|
|
||
| # Running Violence Evaluator on single input row | ||
| violence_score = violence_eval(query="what's the capital of france", response="Paris") | ||
| print(violence_score) | ||
| # </evaluate_violence> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| import os | ||
| from azure.ai.projects import AIProjectClient | ||
| from azure.identity import DefaultAzureCredential | ||
|
|
||
| from dotenv import load_dotenv | ||
|
|
||
| load_dotenv() | ||
|
|
||
| project = AIProjectClient.from_connection_string( | ||
| conn_str=os.environ["AIPROJECT_CONNECTION_STRING"], credential=DefaultAzureCredential() | ||
| ) | ||
|
|
||
| # <chat_completion> | ||
| # get a chat inferencing client using the project's default model inferencing endpoint | ||
| chat = project.inference.get_chat_completions_client() | ||
|
|
||
| # run a chat completion using the inferencing client | ||
| response = chat.complete( | ||
| model="gpt-4o-mini", | ||
| messages=[ | ||
| { | ||
| "role": "system", | ||
| "content": "You are an AI assistant that speaks like a techno punk rocker from 2350. Be cool but not too cool. Ya dig?", | ||
| }, | ||
| {"role": "user", "content": "Hey, can you help me with my taxes? I'm a freelancer."}, | ||
| ], | ||
| ) | ||
|
|
||
| print(response.choices[0].message.content) | ||
| # </chat_completion> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| --- | ||
| name: Chat Prompt | ||
| description: A prompty that extract users query intent based on the current_query and chat_history of the conversation | ||
| model: | ||
| api: chat | ||
| configuration: | ||
| azure_deployment: gpt-4o-mini | ||
| parameters: | ||
| max_tokens: 256 # limit the output | ||
| temperature: 0.8 # higher temperature for creative answers | ||
| --- | ||
| system: | ||
| You are a helpful writing assistant. | ||
| The user's first name is {{first_name}} and their last name is {{last_name}}. | ||
|
|
||
| user: | ||
| Write me a short poem about flowers |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| import os | ||
| from azure.ai.projects import AIProjectClient | ||
| from azure.identity import DefaultAzureCredential | ||
|
|
||
| from dotenv import load_dotenv | ||
|
|
||
| load_dotenv() | ||
|
|
||
| project = AIProjectClient.from_connection_string( | ||
| conn_str=os.environ["AIPROJECT_CONNECTION_STRING"], credential=DefaultAzureCredential() | ||
| ) | ||
|
|
||
| # <get_openai_client> | ||
| openai = project.inference.get_azure_openai_client(api_version="2024-06-01") | ||
| response = openai.chat.completions.create( | ||
| model="gpt-4o", | ||
| messages=[ | ||
| {"role": "system", "content": "You are a helpful writing assistant"}, | ||
| {"role": "user", "content": "Write me a poem about flowers"}, | ||
| ], | ||
| ) | ||
|
|
||
| print(response.choices[0].message.content) | ||
| # </get_openai_client> |
Binary file added
BIN
+113 KB
scenarios/projects/basic/packages/azure_ai_inference-1.0.0b5-py3-none-any.whl
Binary file not shown.
Binary file added
BIN
+150 KB
scenarios/projects/basic/packages/azure_ai_projects-1.0.0b1-py3-none-any.whl
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| # Information about product item_number: 1 | ||
|
|
||
| ## Brand | ||
| Contoso Galaxy Innovations | ||
|
|
||
| ## Category | ||
| Smart Eyewear | ||
|
|
||
| ## Features | ||
| - Augmented Reality interface | ||
| - Voice-controlled AI assistant | ||
| - HD video recording with 3D audio | ||
| - UV protection and blue light filtering | ||
| - Wireless charging with extended battery life | ||
|
|
||
| ## User Guide | ||
|
|
||
| ### 1. Introduction | ||
| Introduction to your new SmartView Glasses | ||
|
|
||
| ### 2. Product Overview | ||
| Overview of features and controls | ||
|
|
||
| ### 3. Sizing and Fit | ||
| Finding your perfect fit and style adjustments | ||
|
|
||
| ### 4. Proper Care and Maintenance | ||
| Cleaning and caring for your SmartView Glasses | ||
|
|
||
| ### 5. Break-in Period | ||
| Adjusting to the augmented reality experience | ||
|
|
||
| ### 6. Safety Tips | ||
| Safety guidelines for public and private spaces | ||
|
|
||
| ### 7. Troubleshooting | ||
| Quick fixes for common issues | ||
|
|
||
| ## Warranty Information | ||
| Two-year limited warranty on all electronic components | ||
|
|
||
| ## Contact Information | ||
| Customer Support at [email protected] | ||
|
|
||
| ## Return Policy | ||
| 30-day return policy with no questions asked | ||
|
|
||
| ## FAQ | ||
| - How to sync your SmartView Glasses with your devices | ||
| - Troubleshooting connection issues | ||
| - Customizing your augmented reality environment |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| from azure.identity import DefaultAzureCredential | ||
| from azure.ai.projects import AIProjectClient | ||
|
|
||
| project_connection_string = "your_connection_string" | ||
|
|
||
| project = AIProjectClient.from_connection_string( | ||
| conn_str=project_connection_string, credential=DefaultAzureCredential() | ||
| ) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we add a README.md for this sample?