Skip to content

Commit

Permalink
Update code examples
Browse files Browse the repository at this point in the history
  • Loading branch information
mythz committed Nov 25, 2024
1 parent 2118b57 commit 220e7d8
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 14 deletions.
12 changes: 4 additions & 8 deletions MyApp/_includes/ai-server/cs/ai-server-compatible-1.cs.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
```csharp
var apiClient = GetLocalApiClient(AiServerUrl);
apiClient.BearerToken = Environment.GetEnvironmentVariable("AI_SERVER_API_KEY");
var client = GetLocalApiClient(AiServerUrl);

var request = new OpenAiChatCompletion {
var response = client.Post(new OpenAiChatCompletion {
Model = "llama3.1:8b",
Messages =
[
new() { Role = "system", Content = "You are a helpful AI assistant." },
new() { Role = "user", Content = "How do LLMs work?" }
],
MaxTokens = 50
};

var response = await apiClient.PostAsync(request);
var openAiResponse = response; // The same
Console.WriteLine(openAiResponse.Choices[0].Message.Content);
});
var answer = response.Choices[0].Message.Content;
```
16 changes: 11 additions & 5 deletions MyApp/_includes/ai-server/cs/queue-openai-chat-completion-1.cs.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
```csharp
var client = GetLocalApiClient(AiServerUrl);
client.BearerToken = Environment.GetEnvironmentVariable("AI_SERVER_API_KEY");

var api = await client.ApiAsync(new QueueOpenAiChatCompletion
var response = client.Post(new QueueOpenAiChatCompletion
{
Request = new()
{
Expand All @@ -15,7 +14,14 @@ var api = await client.ApiAsync(new QueueOpenAiChatCompletion
MaxTokens = 50
},
});
api.ThrowIfError();
// Response only returns the related job information
Console.WriteLine($"RefId: {api.Response.RefId}, JobId: {api.Response.Id}");

// Poll for Job Completion Status
GetOpenAiChatStatusResponse status = new();
while (status.JobState is BackgroundJobState.Started or BackgroundJobState.Queued)
{
status = await client.GetAsync(new GetOpenAiChatStatus { RefId = response.RefId });
await Task.Delay(1000);
}

var answer = status.Result.Choices[0].Message.Content;
```
10 changes: 10 additions & 0 deletions MyApp/_includes/ai-server/cs/queue-speech-to-text-1.cs.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,14 @@
using var fsAudio = File.OpenRead("files/test_audio.wav");
var response = client.PostFileWithRequest(new QueueSpeechToText(),
new UploadFile("test_audio.wav", fsAudio, "audio"));

// Poll for Job Completion Status
GetTextGenerationStatusResponse status = new();
while (status.JobState is BackgroundJobState.Started or BackgroundJobState.Queued)
{
status = client.Get(new GetTextGenerationStatus { RefId = response.RefId });
Thread.Sleep(1000);
}

var answer = status.Results[0].Text;
```
10 changes: 10 additions & 0 deletions MyApp/_includes/ai-server/cs/queue-text-to-speech-1.cs.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,14 @@ var response = client.PostFileWithRequest(new QueueTextToSpeech {
Text = "Hello, how are you?"
},
new UploadFile("test_audio.wav", fsAudio, "audio"));

GetArtifactGenerationStatusResponse status = new();
while (status.JobState is BackgroundJobState.Started or BackgroundJobState.Queued)
{
status = client.Get(new GetArtifactGenerationStatus { RefId = response.RefId });
Thread.Sleep(1000);
}

// Download the watermarked image
File.WriteAllBytes(saveToPath, status.Results[0].Url.GetBytesFromUrl());
```
2 changes: 1 addition & 1 deletion MyApp/_includes/ai-server/cs/text-to-speech-1.cs.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ var response = client.PostFileWithRequest(new TextToSpeech {
},
new UploadFile("test_audio.wav", fsAudio, "audio"));

response.Results[0].Url.DownloadFileTo(outputFileName);
File.WriteAllBytes(saveToPath, response.Results[0].Url.GetBytesFromUrl());
```

0 comments on commit 220e7d8

Please sign in to comment.