@@ -11,25 +11,21 @@ The following code example shows how to implement this pattern.
11
11
::: zone pivot="python"
12
12
13
13
``` python
14
- import os
15
- from dotenv import load_dotenv
16
14
from azure.identity import DefaultAzureCredential
17
15
from azure.ai.projects import AIProjectClient
18
16
from azure.ai.projects.models import ConnectionType
19
17
import openai
20
18
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" )
26
19
27
20
# Initialize the project client
28
21
projectClient = AIProjectClient.from_connection_string(
29
- conn_str = project_connection ,
22
+ conn_str = " <region>.api.azureml.ms;<project_id>;<hub_name>;<project_name> " ,
30
23
credential = DefaultAzureCredential()
31
24
)
32
25
26
+ # Get an Azure OpenAI chat client
27
+ chat_client = projectClient.inference.get_azure_openai_client(api_version = " 2024-10-21" )
28
+
33
29
# Use the AI search service connection to get service details
34
30
searchConnection = projectClient.connections.get_default(
35
31
connection_type = ConnectionType.AZURE_AI_SEARCH ,
@@ -38,16 +34,13 @@ searchConnection = projectClient.connections.get_default(
38
34
search_url = searchConnection.endpoint_url
39
35
search_key = searchConnection.key
40
36
41
- # Get an Azure OpenAI chat client
42
- chat_client = projectClient.inference.get_azure_openai_client(api_version = " 2024-10-21" )
43
-
44
37
# Initialize prompt with system message
45
38
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 ." }
47
40
]
48
41
49
42
# Add a user input message to the prompt
50
- input_text = input (" Enter a travel-related question: " )
43
+ input_text = input (" Enter a question: " )
51
44
prompt.append({" role" : " user" , " content" : input_text})
52
45
53
46
# Additional parameters to apply RAG pattern using the AI Search index
@@ -57,7 +50,7 @@ rag_params = {
57
50
" type" : " azure_search" ,
58
51
" parameters" : {
59
52
" endpoint" : search_url,
60
- " index_name" : index_name ,
53
+ " index_name" : " <azure_ai_search_index_name> " ,
61
54
" authentication" : {
62
55
" type" : " api_key" ,
63
56
" key" : search_key,
@@ -69,7 +62,7 @@ rag_params = {
69
62
70
63
# Submit the prompt with the index information
71
64
response = chat_client.chat.completions.create(
72
- model = model_deployment ,
65
+ model = " <model_deployment_name> " ,
73
66
messages = prompt,
74
67
extra_body = rag_params
75
68
)
@@ -84,12 +77,6 @@ print(completion)
84
77
::: zone pivot="csharp"
85
78
86
79
``` 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 ;
93
80
using Azure .Identity ;
94
81
using Azure .AI .Projects ;
95
82
using Azure .AI .OpenAI ;
@@ -100,15 +87,15 @@ using OpenAI.Chat;
100
87
.. .
101
88
102
89
{
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" ];
109
90
110
91
// 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>" );
112
99
113
100
// Use the AI search service connection to get service details
114
101
var connectionsClient = projectClient .GetConnectionsClient ();
@@ -117,28 +104,29 @@ using OpenAI.Chat;
117
104
string search_url = searchProperties .Target ;
118
105
string search_key = searchProperties .Credentials .Key ;
119
106
120
- // Get an Azure OpenAI chat client
121
- ChatClient chatClient = projectClient .GetAzureOpenAIChatClient (model_deployment );
107
+
122
108
123
109
// Initialize prompt with system message
124
110
var prompt = new List <ChatMessage >()
125
111
{
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 ." )
127
113
};
128
114
129
115
// Add a user input message to the prompt
130
- Console .WriteLine (" Enter a travel-related question: " );
116
+ Console .WriteLine (" Enter a question: " );
131
117
input_text = Console .ReadLine ();
132
118
prompt .Add (new UserChatMessage (input_text ));
133
119
134
120
// Additional parameters to apply RAG pattern using the AI Search index
135
121
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
+ );
142
130
143
131
// Submit the prompt with the index information
144
132
ChatCompletion completion = chatClient .CompleteChat (prompt , options );
@@ -150,3 +138,56 @@ using OpenAI.Chat;
150
138
```
151
139
152
140
::: 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