@@ -158,7 +158,10 @@ Create a project client in code:
158158
159159# [Sync](#tab/sync)
160160
161- :::code language=" csharp" source=" ~/azureai-samples-csharp/scenarios/projects/basic-csharp/Program.cs" id=" snippet_get_project" :::
161+ ` ` ` csharp
162+ var connectionString = " <your_connection_string>" ;
163+ var projectClient = new AIProjectClient(connectionString, new DefaultAzureCredential ());
164+ ` ` `
162165
163166# [Async](#tab/async)
164167
@@ -226,7 +229,33 @@ using Azure.AI.OpenAI;
226229
227230If you have existing code that uses the OpenAI SDK, you can use the project client to create an ` AzureOpenAI` client that uses your project' s Azure OpenAI connection:
228231
229- :::code language="csharp" source="~/azureai-samples-csharp/scenarios/projects/basic-csharp/Program.cs" id="azure_openai":::
232+ ```csharp
233+ var connections = projectClient.GetConnectionsClient();
234+ ConnectionResponse connection = connections.GetDefaultConnection(ConnectionType.AzureOpenAI, withCredential: true);
235+ var properties = connection.Properties as ConnectionPropertiesApiKeyAuth;
236+
237+ if (properties == null) {
238+ throw new Exception("Invalid auth type, expected API key auth");
239+ }
240+
241+ // Create and use an Azure OpenAI client
242+ AzureOpenAIClient azureOpenAIClient = new(
243+ new Uri(properties.Target),
244+ new AzureKeyCredential(properties.Credentials.Key));
245+
246+ // This must match the custom deployment name you chose for your model
247+ ChatClient chatClient = azureOpenAIClient.GetChatClient("gpt-4o-mini");
248+
249+ ChatCompletion completion = chatClient.CompleteChat(
250+ [
251+ new SystemChatMessage("You are a helpful assistant that talks like a pirate."),
252+ new UserChatMessage("Does Azure OpenAI support customer managed keys?"),
253+ new AssistantChatMessage("Yes, customer managed keys are supported by Azure OpenAI"),
254+ new UserChatMessage("Do other Azure AI services support this too?")
255+ ]);
256+
257+ Console.WriteLine($"{completion.Role}: {completion.Content[0].Text}");
258+ ```
230259
231260::: zone-end
232261
@@ -280,7 +309,25 @@ using Azure.AI.Inference;
280309
281310You can use the project client to get a configured and authenticated ` ChatCompletionsClient` or ` EmbeddingsClient` :
282311
283- :::code language=" csharp" source=" ~/azureai-samples-csharp/scenarios/projects/basic-csharp/Program.cs" id=" snippet_inference" :::
312+ ` ` ` csharp
313+ var connectionString = Environment.GetEnvironmentVariable(" AIPROJECT_CONNECTION_STRING" );
314+ var projectClient = new AIProjectClient(connectionString, new DefaultAzureCredential ());
315+
316+ ChatCompletionsClient chatClient = projectClient.GetChatCompletionsClient ();
317+
318+ var requestOptions = new ChatCompletionsOptions ()
319+ {
320+ Messages =
321+ {
322+ new ChatRequestSystemMessage(" You are a helpful assistant." ),
323+ new ChatRequestUserMessage(" How many feet are in a mile?" ),
324+ },
325+ Model = " gpt-4o-mini"
326+ };
327+
328+ Response< ChatCompletions> response = chatClient.Complete(requestOptions);
329+ Console.WriteLine(response.Value.Content);
330+ ` ` `
284331
285332::: zone-end
286333
@@ -405,7 +452,21 @@ using Azure.Search.Documents.Models;
405452
406453Instantiate the search and/or search index client as desired:
407454
408- :::code language=" csharp" source=" ~/azureai-samples-csharp/scenarios/projects/basic-csharp/Program.cs" id=" azure_aisearch" :::
455+ ` ` ` csharp
456+ var connections = projectClient.GetConnectionsClient ();
457+ var connection = connections.GetDefaultConnection(ConnectionType.AzureAISearch, withCredential: true).Value;
458+
459+ var properties = connection.Properties as ConnectionPropertiesApiKeyAuth;
460+ if (properties == null) {
461+ throw new Exception(" Invalid auth type, expected API key auth" );
462+ }
463+
464+ SearchClient searchClient = new SearchClient(
465+ new Uri(properties.Target),
466+ " products" ,
467+ new AzureKeyCredential(properties.Credentials.Key));
468+ ` ` `
469+
409470
410471::: zone-end
411472
0 commit comments