Skip to content

Commit 612313f

Browse files
authored
pull base content,head:MicrosoftDocs:main,into:wwlpublishsync
2 parents 1d403f6 + 33bf3ea commit 612313f

File tree

1 file changed

+79
-38
lines changed

1 file changed

+79
-38
lines changed

learn-pr/wwl-data-ai/build-copilot-ai-studio/includes/3b-openai-client.md

Lines changed: 79 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -11,25 +11,21 @@ The following code example shows how to implement this pattern.
1111
::: zone pivot="python"
1212

1313
```python
14-
import os
15-
from dotenv import load_dotenv
1614
from azure.identity import DefaultAzureCredential
1715
from azure.ai.projects import AIProjectClient
1816
from azure.ai.projects.models import ConnectionType
1917
import openai
2018

21-
# Get configuration settings
22-
load_dotenv()
23-
project_connection = os.getenv("PROJECT_CONNECTION")
24-
model_deployment = os.getenv("MODEL_DEPLOYMENT")
25-
index_name = os.getenv("INDEX_NAME")
2619

2720
# Initialize the project client
2821
projectClient = AIProjectClient.from_connection_string(
29-
conn_str=project_connection,
22+
conn_str="<region>.api.azureml.ms;<project_id>;<hub_name>;<project_name>",
3023
credential=DefaultAzureCredential()
3124
)
3225

26+
# Get an Azure OpenAI chat client
27+
chat_client = projectClient.inference.get_azure_openai_client(api_version="2024-10-21")
28+
3329
# Use the AI search service connection to get service details
3430
searchConnection = projectClient.connections.get_default(
3531
connection_type=ConnectionType.AZURE_AI_SEARCH,
@@ -38,16 +34,13 @@ searchConnection = projectClient.connections.get_default(
3834
search_url = searchConnection.endpoint_url
3935
search_key = searchConnection.key
4036

41-
# Get an Azure OpenAI chat client
42-
chat_client = projectClient.inference.get_azure_openai_client(api_version="2024-10-21")
43-
4437
# Initialize prompt with system message
4538
prompt = [
46-
{"role": "system", "content": "You are a travel assistant that provides information on travel services available from Margie's Travel."}
39+
{"role": "system", "content": "You are a helpful AI assistant."}
4740
]
4841

4942
# Add a user input message to the prompt
50-
input_text = input("Enter a travel-related question: ")
43+
input_text = input("Enter a question: ")
5144
prompt.append({"role": "user", "content": input_text})
5245

5346
# Additional parameters to apply RAG pattern using the AI Search index
@@ -57,7 +50,7 @@ rag_params = {
5750
"type": "azure_search",
5851
"parameters": {
5952
"endpoint": search_url,
60-
"index_name": index_name,
53+
"index_name": "<azure_ai_search_index_name>",
6154
"authentication": {
6255
"type": "api_key",
6356
"key": search_key,
@@ -69,7 +62,7 @@ rag_params = {
6962

7063
# Submit the prompt with the index information
7164
response = chat_client.chat.completions.create(
72-
model=model_deployment,
65+
model="<model_deployment_name>",
7366
messages=prompt,
7467
extra_body=rag_params
7568
)
@@ -84,12 +77,6 @@ print(completion)
8477
::: zone pivot="csharp"
8578

8679
```csharp
87-
using System;
88-
using Azure;
89-
using System.IO;
90-
using System.Text;
91-
using System.Collections.Generic;
92-
using Microsoft.Extensions.Configuration;
9380
using Azure.Identity;
9481
using Azure.AI.Projects;
9582
using Azure.AI.OpenAI;
@@ -100,15 +87,15 @@ using OpenAI.Chat;
10087
...
10188

10289
{
103-
// Get configuration settings
104-
IConfigurationBuilder builder = new ConfigurationBuilder().AddJsonFile("appsettings.json");
105-
IConfigurationRoot configuration = builder.Build();
106-
string project_connection = configuration["PROJECT_CONNECTION"];
107-
string model_deployment = configuration["MODEL_DEPLOYMENT"];
108-
string index_name = configuration["INDEX_NAME"];
10990

11091
// Initialize the project client
111-
var projectClient = new AIProjectClient(project_connection, new DefaultAzureCredential());
92+
var projectClient = new AIProjectClient(
93+
"<region>.api.azureml.ms;<project_id>;<hub_name>;<project_name>",
94+
new DefaultAzureCredential()
95+
);
96+
97+
// Get an Azure OpenAI chat client
98+
ChatClient chatClient = projectClient.GetAzureOpenAIChatClient("<model_deployment_name>");
11299

113100
// Use the AI search service connection to get service details
114101
var connectionsClient = projectClient.GetConnectionsClient();
@@ -117,28 +104,29 @@ using OpenAI.Chat;
117104
string search_url = searchProperties.Target;
118105
string search_key = searchProperties.Credentials.Key;
119106

120-
// Get an Azure OpenAI chat client
121-
ChatClient chatClient = projectClient.GetAzureOpenAIChatClient(model_deployment);
107+
122108

123109
// Initialize prompt with system message
124110
var prompt = new List<ChatMessage>()
125111
{
126-
new SystemChatMessage("You are a travel assistant that provides information on travel services available from Margie's Travel.")
112+
new SystemChatMessage("You are a helpful AI assistant.")
127113
};
128114

129115
// Add a user input message to the prompt
130-
Console.WriteLine("Enter a travel-related question: ");
116+
Console.WriteLine("Enter a question: ");
131117
input_text = Console.ReadLine();
132118
prompt.Add(new UserChatMessage(input_text));
133119

134120
// Additional parameters to apply RAG pattern using the AI Search index
135121
ChatCompletionOptions options = new();
136-
options.AddDataSource(new AzureSearchChatDataSource()
137-
{
138-
Endpoint = new Uri(search_url),
139-
IndexName = index_name,
140-
Authentication = DataSourceAuthentication.FromApiKey(search_key),
141-
});
122+
options.AddDataSource(
123+
new AzureSearchChatDataSource()
124+
{
125+
Endpoint = new Uri(search_url),
126+
IndexName = "<azure_ai_search_index_name>",
127+
Authentication = DataSourceAuthentication.FromApiKey(search_key),
128+
}
129+
);
142130

143131
// Submit the prompt with the index information
144132
ChatCompletion completion = chatClient.CompleteChat(prompt, options);
@@ -150,3 +138,56 @@ using OpenAI.Chat;
150138
```
151139

152140
::: zone-end
141+
142+
In this example, the search against the index is *keyword-based* - in other words, the query consists of the text in the user prompt, which is matched to text in the indexed documents. When using an index that supports it, an alternative approach is to use a *vector-based* query in which the index and the query use numeric vectors to represent text tokens. Searching with vectors enables matching based on semantic similarity as well as literal text matches.
143+
144+
To use a vector-based query, you can modify the specification of the Azure AI Search data source details to include an embedding model; which is then used to vectorize the query text.
145+
146+
::: zone pivot="python"
147+
148+
```python
149+
rag_params = {
150+
"data_sources": [
151+
{
152+
"type": "azure_search",
153+
"parameters": {
154+
"endpoint": search_url,
155+
"index_name": "<azure_ai_search_index_name>",
156+
"authentication": {
157+
"type": "api_key",
158+
"key": search_key,
159+
},
160+
# Params for vector-based query
161+
"query_type": "vector",
162+
"embedding_dependency": {
163+
"type": "deployment_name",
164+
"deployment_name": "<embedding_model_deployment_name>",
165+
},
166+
}
167+
}
168+
],
169+
}
170+
```
171+
172+
::: zone-end
173+
174+
::: zone pivot="csharp"
175+
176+
```csharp
177+
{
178+
ChatCompletionOptions options = new();
179+
options.AddDataSource(
180+
new AzureSearchChatDataSource()
181+
{
182+
Endpoint = new Uri(search_url),
183+
IndexName = "<azure_ai_search_index_name>",
184+
Authentication = DataSourceAuthentication.FromApiKey(search_key),
185+
// Params for vector-based query
186+
QueryType = "vector",
187+
VectorizationSource = DataSourceVectorizer.FromDeploymentName("<embedding_model_deployment_name>"),
188+
},
189+
);
190+
}
191+
```
192+
193+
::: zone-end

0 commit comments

Comments
 (0)