Skip to content

Commit 634e85d

Browse files
updated readme to new APIs (Azure#47490)
1 parent 424da93 commit 634e85d

File tree

1 file changed

+26
-185
lines changed

1 file changed

+26
-185
lines changed

sdk/cloudmachine/README.md

Lines changed: 26 additions & 185 deletions
Original file line numberDiff line numberDiff line change
@@ -50,19 +50,20 @@ Open `Program.cs` file and add the following two lines of code to the top of the
5050
```csharp
5151
using Azure.CloudMachine;
5252

53-
if (CloudMachineInfrastructure.Configure(args)) return;
53+
CloudMachineInfrastructure infrastrucutre = new();
54+
if (infrastrucutre.TryExecuteCommand(args)) return;
5455
```
55-
The `CloudMachineInfrastructure.Configure` call allows running the app with a `-bicep` switch, which will generate bicep files required to provision CloudMachine resources in Azure. Let's generate these bicep files now.
56+
57+
The `TryExecuteCommand` call allows running the app with a `-init` switch, which will generate bicep files required to provision CloudMachine resources in Azure. Let's generate these bicep files now.
5658
```dotnetcli
57-
dotnet run -bicep
59+
dotnet run -init
5860
```
5961
As you can see, a folder called `infra` was created with several bicep files in it. Let's now initialize the project.
62+
6063
```dotnetcli
6164
azd init
6265
```
63-
select template, choose 'yes' when asked 'Continue initializing an app here?', choose the 'minimal' template, and use 'cmserver' as the environment name
64-
65-
Once the initialization completes, let's provision the resources. Select `eastus` as the region
66+
type 'demo' as the environment name, and then let's provision the resources (select `eastus` as the region):
6667
```dotnetcli
6768
azd provision
6869
```
@@ -77,213 +78,53 @@ az resource list --resource-group <resource_group_from_command_line> --output ta
7778

7879
#### Use CDK to add resources to the CloudMachine
7980

80-
Since we are writing an AI application, we need to provision Azure OpenAI resources. To do this, add the follwoing class to the end of the `Program.cs` file:
81-
```csharp
82-
class AssistantService {
83-
internal static void Configure(CloudMachineInfrastructure cm) {
84-
cm.AddFeature(new OpenAIFeature() { Chat = new AIModel("gpt-4o-mini", "2024-07-18") });
85-
}
86-
}
87-
```
88-
Then change the configuration call at the beginning of the file to:
81+
Since we are writing an AI application, we need to provision Azure OpenAI resources. To do this, add the follwoing line of code right below where the infrastructure instance is created:
8982
```csharp
90-
if (CloudMachineInfrastructure.Configure(args, AssistantService.Configure)) return;
83+
infrastrucutre.AddFeature(new OpenAIModelFeature("gpt-4o-mini", "2024-07-18"));
9184
```
9285
Now regenerate the bicep files and re-provision
9386
```dotnetcli
94-
dotnet run -bicep
87+
dotnet run -init
9588
azd provision
9689
```
9790

91+
#### Add CloudMachineClient to ASP.NET DI Container
92+
You will be using `CloudMachineClient` to access rources provisioned in the cloud machine. Let's add such client to the DI container such that it is avaliable to ASP.NET handlers
93+
```dotnetcli
94+
builder.AddCloudMachine(infrastrucutre);
95+
```
9896
#### Call CloudMachine APIs
9997

100-
You are now ready to call Azure OpenAI service from the app. To do this, add `CloudMachineClient` field and a `Chat` method to `AssistantService`:
101-
```csharp
102-
class AssistantService {
103-
CloudMachineClient cm = new CloudMachineClient();
104-
105-
public async Task<string> Chat(string message) {
106-
var client = cm.GetOpenAIChatClient();
107-
ChatCompletion completion = await client.CompleteChatAsync(message);
108-
return completion.Content[0].Text;
109-
}
110-
}
111-
```
112-
Lastly, create an instance of the service and call the `Chat` method when the user navigates to the root URL:
98+
You are now ready to call Azure OpenAI service from the app. To do this, change the line of code that maps the application root to the following:
99+
113100
```csharp
114-
var service = new AssistantService();
115-
app.MapGet("/", async () => await service.Chat("List all noble gases"));
101+
app.MapGet("/", (CloudMachineClient cm) => cm.GetOpenAIChatClient().CompleteChat("list all noble gases").AsText());
116102
```
103+
117104
The full program should now look like the following:
118105
```csharp
119106
using Azure.CloudMachine;
120107
using Azure.CloudMachine.OpenAI;
121-
using OpenAI.Chat;
122108

123-
if (CloudMachineInfrastructure.Configure(args, AssistantService.Configure)) return;
109+
CloudMachineInfrastructure infrastrucutre = new();
110+
infrastrucutre.AddFeature(new OpenAIModelFeature("gpt-4o-mini", "2024-07-18"));
111+
if (infrastrucutre.TryExecuteCommand(args)) return;
124112

125113
var builder = WebApplication.CreateBuilder(args);
126-
var app = builder.Build();
127-
128-
var service = new AssistantService();
129-
app.MapGet("/", async () => await service.Chat("List all noble gases"));
130-
131-
app.Run();
132-
133-
class AssistantService {
134-
CloudMachineClient cm = new CloudMachineClient();
135-
136-
public async Task<string> Chat(string message) {
137-
var client = cm.GetOpenAIChatClient();
138-
ChatCompletion completion = await client.CompleteChatAsync(message);
139-
return completion.Content[0].Text;
140-
}
141-
142-
internal static void Configure(CloudMachineInfrastructure cm) {
143-
cm.AddFeature(new OpenAIFeature() {
144-
Chat = new AIModel("gpt-4o-mini", "2024-07-18")
145-
});
146-
}
147-
}
148-
```
149-
You can now start the application
150-
```dotnetcli
151-
dotnet run
152-
```
153-
and navigate to the URL printed in the console.
154-
155-
#### Use TDK to expose Web APIs and generate TypeSpec
156-
First, let's define an API we want to expose. We will do it using a C# interface. Add the following interface to the end of `Program.cs`:
157-
```csharp
158-
interface IAssistantService {
159-
Task<string> Chat(string message);
160-
}
161-
```
162-
Make sure that the `AssistantService` class implements the interface:
163-
```csharp
164-
class AssistantService : IAssistantService
165-
```
166-
Expose the service methods as web APIs by adding the following line after the existing `var service = new AssistantService();` line:
167-
```csharp
168-
app.Map(service);
169-
```
170-
Lastly, add the ability to generate TypeSpec for the new API by adding a new statement to the `Configure` method
171-
```csharp
172-
cm.AddEndpoints<IAssistantService>();
173-
```
174-
Your program shoud now look like the following:
175-
```csharp
176-
using Azure.CloudMachine;
177-
using Azure.CloudMachine.OpenAI;
178-
using OpenAI.Chat;
179-
180-
if (CloudMachineInfrastructure.Configure(args, AssistantService.Configure)) return;
114+
builder.AddCloudMachine(infrastrucutre);
181115

182-
var builder = WebApplication.CreateBuilder(args);
183116
var app = builder.Build();
184117

185-
var service = new AssistantService();
186-
app.Map(service);
187-
app.MapGet("/", async () => await service.Chat("List all noble gases"));
118+
app.MapGet("/", (CloudMachineClient cm) => cm.GetOpenAIChatClient().CompleteChat("list all noble gases").AsText());
188119

189120
app.Run();
190-
191-
class AssistantService : IAssistantService {
192-
CloudMachineClient cm = new CloudMachineClient();
193-
194-
public async Task<string> Chat(string message) {
195-
var client = cm.GetOpenAIChatClient();
196-
ChatCompletion completion = await client.CompleteChatAsync(message);
197-
return completion.Content[0].Text;
198-
}
199-
200-
internal static void Configure(CloudMachineInfrastructure cm) {
201-
cm.AddFeature(new OpenAIFeature() {
202-
Chat = new AIModel("gpt-4o-mini", "2024-07-18")
203-
});
204-
cm.AddEndpoints<IAssistantService>();
205-
}
206-
}
207-
208-
interface IAssistantService {
209-
Task<string> Chat(string message);
210-
}
211121
```
212-
You can now start the application and browse to the /chat endpoint [TBD on how to pass the parameter]
213122

214-
But what's more interesting, you can run the app with the -tsp switch to generate TypeSpec for the endpoint:
215-
```dotnetcli
216-
dotnet run -tsp
217-
```
218-
This will create a `tsp` directory, `AssistantService.tsp` file, with the following contents:
219-
```tsp
220-
import "@typespec/http";
221-
import "@typespec/rest";
222-
import "@azure-tools/typespec-client-generator-core";
223-
224-
@service({
225-
title: "AssistantService",
226-
})
227-
228-
namespace AssistantService;
229-
230-
using TypeSpec.Http;
231-
using TypeSpec.Rest;
232-
using Azure.ClientGenerator.Core;
233-
234-
@client interface AssistantServiceClient {
235-
@get @route("chat") Chat(@body message: string) : {
236-
@statusCode statusCode: 200;
237-
@body response : string;
238-
};
239-
}
240-
```
241-
#### Generate client libraries from TypeSpec
242-
Let's now generate and build a C# client library from the `AssistantService.tsp` file:
243-
```dotnetcli
244-
cd ..
245-
npm install @typespec/http-client-csharp
246-
tsp compile .\server\tsp\AssistantService.tsp --emit "@typespec/http-client-csharp"
247-
dotnet build tsp-output\@typespec\http-client-csharp\src\AssistantService.csproj
248-
```
249-
You can also generate libraries for other languages, e.g.
250-
```dotnetcli
251-
npm install @typespec/http-client-python
252-
tsp compile .\server\tsp\AssistantService.tsp --emit "@typespec/http-client-python"
253-
```
254-
255-
#### Create command line client app for the service
256-
```dotnetcli
257-
mkdir cmdclient
258-
cd cmdclient
259-
dotnet new console
260-
dotnet add reference ..\tsp-output\@typespec\http-client-csharp\src\AssistantService.csproj
261-
```
262-
And change the `Program.cs` file to the following, replacing the client URI with the URI in your server's launchsettings.json file ('cmdemo\server\Properties' folder)
263-
264-
```csharp
265-
using AssistantService;
266-
267-
var client = new AssistantServiceClient(new Uri("http://localhost:5121/"));
268-
269-
while(true){
270-
string message = Console.ReadLine();
271-
var completion = client.Chat(message);
272-
Console.WriteLine(completion);
273-
}
274-
```
275-
Now start the server
276-
```dotnetcli
277-
start cmd
278-
cd..
279-
cd server
280-
dotnet run
281-
```
282-
Go back to the client project command window and start the client:
123+
You can now start the application
283124
```dotnetcli
284125
dotnet run
285126
```
286-
You can use the simple command line app to ask Azure OpenAI some questions.
127+
and navigate to the URL printed in the console.
287128

288129
## Examples
289130

0 commit comments

Comments
 (0)