generated from NetCoreTemplates/razor-ssg
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
36 changed files
with
209 additions
and
167 deletions.
There are no files selected for viewing
23 changes: 13 additions & 10 deletions
23
MyApp/_includes/ai-server/cs/ai-server-compatible-1.cs.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,18 @@ | ||
```csharp | ||
var apiClient = GetLocalApiClient("https://localhost:5005"); | ||
var apiClient = GetLocalApiClient(AiServerUrl); | ||
apiClient.BearerToken = Environment.GetEnvironmentVariable("AI_SERVER_API_KEY"); | ||
|
||
var response = await apiClient.PostAsync(new OpenAiChatCompletion { | ||
Model = "llama3:8b", | ||
Messages = new List<OpenAiMessage> | ||
{ | ||
new OpenAiMessage { Role = "system", Content = "You are a helpful AI assistant." }, | ||
new OpenAiMessage { Role = "user", Content = "How do LLMs work?" } | ||
}, | ||
var request = 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 | ||
}); | ||
Console.WriteLine(response.Choices[0].Message.Content); | ||
}; | ||
|
||
var response = await apiClient.PostAsync(request); | ||
var openAiResponse = response; // The same | ||
Console.WriteLine(openAiResponse.Choices[0].Message.Content); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,10 @@ | ||
```csharp | ||
var response = client.PostFilesWithRequest(new ConvertImage { | ||
using var fsImage = File.OpenRead("files/test_image.jpg"); | ||
var response = client.PostFileWithRequest(new ConvertImage { | ||
OutputFormat = ImageOutputFormat.Gif | ||
}, | ||
[new UploadFile("test_image.jpg", File.OpenRead("files/test_image.jpg"), "image")] | ||
); | ||
new UploadFile("test_image.jpg", fsImage, "image")); | ||
|
||
var videoUrl = response.Outputs[0].Url; | ||
var videoUrl = response.Results[0].Url; | ||
videoUrl.DownloadFileTo(outputFileName); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,10 @@ | ||
```csharp | ||
var response = client.PostFilesWithRequest(new ConvertVideo { | ||
using var fsVideo = File.OpenRead("files/test_video.webm"); | ||
var response = client.PostFileWithRequest(new ConvertVideo { | ||
OutputFormat = ConvertVideoOutputFormat.MOV | ||
}, | ||
[new UploadFile("test_video.webm", File.OpenRead("files/test_video.webm"), "video")] | ||
); | ||
new UploadFile("test_video.webm", fsVideo, "video")); | ||
|
||
var videoUrl = response.Outputs[0].Url; | ||
var videoUrl = response.Results[0].Url; | ||
videoUrl.DownloadFileTo(outputFileName); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,13 @@ | ||
```csharp | ||
var response = client.PostFilesWithRequest(new CropImage { | ||
using var fsImage = File.OpenRead("files/test_image.jpg"); | ||
var response = client.PostFileWithRequest(new CropImage { | ||
X = 50, | ||
Y = 50, | ||
Width = 150, | ||
Height = 150 | ||
}, | ||
[new UploadFile("test_image.jpg", File.OpenRead("files/test_image.jpg"), "image")] | ||
); | ||
new UploadFile("test_image.jpg", fsImage, "image")); | ||
|
||
var videoUrl = response.Outputs[0].Url; | ||
var videoUrl = response.Results[0].Url; | ||
videoUrl.DownloadFileTo(outputFileName); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,13 @@ | ||
```csharp | ||
var response = client.PostFilesWithRequest(new CropVideo { | ||
using var fsVideo = File.OpenRead("files/test_video.mp4"); | ||
var response = client.PostFileWithRequest(new CropVideo { | ||
X = 100, | ||
Y = 100, | ||
Width = 500, | ||
Height = 300 | ||
}, | ||
[new UploadFile("test_video.mp4", File.OpenRead("files/test_video.mp4"), "video")] | ||
); | ||
new UploadFile("test_video.mp4", fsVideo, "video")); | ||
|
||
var videoUrl = response.Outputs[0].Url; | ||
var videoUrl = response.Results[0].Url; | ||
videoUrl.DownloadFileTo(outputFileName); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,10 @@ | ||
```csharp | ||
var response = client.PostFilesWithRequest(new ImageToImage { | ||
using var fsImage = File.OpenRead("files/comfyui_upload_test.png"); | ||
var response = client.PostFileWithRequest(new ImageToImage { | ||
PositivePrompt = "A beautiful sunset over the ocean", | ||
NegativePrompt = "A pixelated, low-quality image" | ||
}, | ||
[new UploadFile("image", File.OpenRead("files/comfyui_upload_test.png"), "image")] | ||
); | ||
response.Outputs[0].Url.DownloadFileTo(outputFileName); | ||
new UploadFile("image", fsImage, "image")); | ||
|
||
response.Results[0].Url.DownloadFileTo(outputFileName); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
```csharp | ||
var response = client.PostFilesWithRequest(new ImageToText(), | ||
[new UploadFile("image", File.OpenRead("files/test_image.jpg"), "image")] | ||
); | ||
using var fsImage = File.OpenRead("files/test_image.jpg"); | ||
var response = client.PostFileWithRequest(new ImageToText(), | ||
new UploadFile("image", fsImage, "image")); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
```csharp | ||
var response = client.PostFilesWithRequest(new ImageUpscale(), | ||
[new UploadFile("image", File.OpenRead("files/test_image.jpg"), "image")] | ||
); | ||
response.Outputs[0].Url.DownloadFileTo(outputFileName); | ||
using var fsImage = File.OpenRead("files/test_image.jpg"); | ||
var response = client.PostFileWithRequest(new ImageUpscale(), | ||
new UploadFile("image", fsImage, "image")); | ||
|
||
response.Results[0].Url.DownloadFileTo(outputFileName); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,13 @@ | ||
```csharp | ||
using var fsImage = File.OpenRead("files/comfyui_upload_test.png"); | ||
using var fsMask = File.OpenRead("files/comfyui_upload_test_mask.png"); | ||
var response = client.PostFilesWithRequest(new ImageWithMask { | ||
PositivePrompt = "A beautiful sunset over the ocean", | ||
NegativePrompt = "A pixelated, low-quality image" | ||
}, | ||
[new UploadFile("image", File.OpenRead("files/comfyui_upload_test.png"), "image"), | ||
new UploadFile("mask", File.OpenRead("files/comfyui_upload_test_mask.png"), "mask")] | ||
); | ||
response.Outputs[0].Url.DownloadFileTo(outputFileName); | ||
}, [ | ||
new UploadFile("image", fsImage, "image"), | ||
new UploadFile("mask", fsMask, "mask") | ||
]); | ||
|
||
response.Results[0].Url.DownloadFileTo(outputFileName); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,18 @@ | ||
```csharp | ||
var client = new JsonApiClient("https://api.openai.com/v1"); | ||
var client = new JsonApiClient("https://api.openai.com"); | ||
client.AddHeader("Authorization", "Bearer " + Environment.GetEnvironmentVariable("OPENAI_API_KEY")); | ||
|
||
// Using AI Server DTOs with OpenAI API | ||
var response = await client.PostAsync<OpenAiChatResponse>("/chat/completions", | ||
new OpenAiChatCompletion { | ||
Model = "gpt-4-turbo", | ||
Messages = new List<OpenAiMessage> { | ||
new OpenAiMessage { Role = "system", Content = "You are a helpful AI assistant." }, | ||
new OpenAiMessage { Role = "user", Content = "How do LLMs work?" } | ||
}, | ||
MaxTokens = 50 | ||
}); | ||
var request = new OpenAiChatCompletion { | ||
Model = "gpt-4-turbo", | ||
Messages = [ | ||
new() { Role = "system", Content = "You are a helpful AI assistant." }, | ||
new() { Role = "user", Content = "What is the capital of France?" } | ||
], | ||
MaxTokens = 20 | ||
}; | ||
|
||
var response = await client.PostAsync<OpenAiChatResponse>( | ||
"/v1/chat/completions", | ||
request); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
```csharp | ||
var client = new JsonApiClient(baseUrl); | ||
client.BearerToken = apiKey; | ||
|
||
var api = await client.ApiAsync(new OpenAiChatCompletion { | ||
Model = "mixtral:8x22b", | ||
Messages = [ | ||
new() { | ||
Role = "user", | ||
Content = "What's the capital of France?" | ||
} | ||
], | ||
MaxTokens = 50 | ||
}); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,18 @@ | ||
```csharp | ||
var response = client.PostFilesWithRequest(new QueueConvertImage { | ||
using var fsImage = File.OpenRead("files/test_image.jpg"); | ||
var response = client.PostFileWithRequest(new QueueConvertImage { | ||
OutputFormat = ImageOutputFormat.Png | ||
}, | ||
[new UploadFile("test_image.jpg", File.OpenRead("files/test_image.jpg"), "image")] | ||
); | ||
new UploadFile("test_image.jpg", fsImage, "image")); | ||
|
||
var status = await client.GetAsync(new GetJobStatus { RefId = response.RefId }); | ||
GetArtifactGenerationStatusResponse status = new(); | ||
while (status.JobState is BackgroundJobState.Started or BackgroundJobState.Queued) | ||
{ | ||
status = await client.GetAsync(new GetArtifactGenerationStatus { RefId = response.RefId }); | ||
await Task.Delay(1000); | ||
status = await client.GetAsync(new GetJobStatus { RefId = response.RefId }); | ||
} | ||
|
||
// Download the converted video | ||
var videoUrl = status.Outputs[0].Url; | ||
var videoUrl = status.Results[0].Url; | ||
videoUrl.DownloadFileTo(outputFileName); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,21 @@ | ||
```csharp | ||
var response = client.PostFilesWithRequest(new QueueCropImage { | ||
using var fsImage = File.OpenRead("files/test_image.jpg"); | ||
var response = client.PostFileWithRequest(new QueueCropImage { | ||
X = 50, | ||
Y = 50, | ||
Width = 150, | ||
Height = 150 | ||
}, | ||
[new UploadFile("test_image.jpg", File.OpenRead("files/test_image.jpg"), "image")] | ||
); | ||
new UploadFile("test_image.jpg", fsImage, "image")); | ||
|
||
var status = await client.GetAsync(new GetJobStatus { RefId = response.RefId }); | ||
GetArtifactGenerationStatusResponse status = new(); | ||
while (status.JobState is BackgroundJobState.Started or BackgroundJobState.Queued) | ||
{ | ||
status = await client.GetAsync(new GetArtifactGenerationStatus { RefId = response.RefId }); | ||
await Task.Delay(1000); | ||
status = await client.GetAsync(new GetJobStatus { RefId = response.RefId }); | ||
} | ||
|
||
// Download the cropped video | ||
var videoUrl = status.Outputs[0].Url; | ||
var videoUrl = status.Results[0].Url; | ||
videoUrl.DownloadFileTo($"cropped-image-{status.RefId}.jpg"); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,22 @@ | ||
```csharp | ||
var response = client.PostFilesWithRequest(new QueueCropVideo { | ||
using var fsVideo = File.OpenRead("files/test_video.mp4"); | ||
var response = client.PostFileWithRequest(new QueueCropVideo { | ||
X = 100, | ||
Y = 100, | ||
Width = 500, | ||
Height = 300 | ||
}, | ||
[new UploadFile("test_video.mp4", File.OpenRead("files/test_video.mp4"), "video")] | ||
new UploadFile("test_video.mp4", fsVideo, "video") | ||
); | ||
|
||
var status = await client.GetAsync(new GetJobStatus { RefId = response.RefId }); | ||
GetArtifactGenerationStatusResponse status = new(); | ||
while (status.JobState is BackgroundJobState.Started or BackgroundJobState.Queued) | ||
{ | ||
status = await client.GetAsync(new GetArtifactGenerationStatus { RefId = response.RefId }); | ||
await Task.Delay(1000); | ||
status = await client.GetAsync(new GetJobStatus { RefId = response.RefId }); | ||
} | ||
|
||
// Download the cropped video | ||
var videoUrl = status.Outputs[0].Url; | ||
var videoUrl = status.Results[0].Url; | ||
videoUrl.DownloadFileTo($"cropped-video-{status.RefId}.mp4"); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
```csharp | ||
var response = client.PostFilesWithRequest(new QueueImageToImage { | ||
using var fsImage = File.OpenRead("files/comfyui_upload_test.png"); | ||
var response = client.PostFileWithRequest(new QueueImageToImage { | ||
PositivePrompt = "A beautiful sunset over the ocean", | ||
NegativePrompt = "A pixelated, low-quality image" | ||
}, | ||
[new UploadFile("image", File.OpenRead("files/comfyui_upload_test.png"), "image")] | ||
); | ||
new UploadFile("image", fsImage, "image")); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
```csharp | ||
var response = client.PostFilesWithRequest(new QueueImageToText(), | ||
[new UploadFile("image", File.OpenRead("files/test_image.jpg"), "image")] | ||
); | ||
using var fsImage = File.OpenRead("files/test_image.jpg"); | ||
var response = client.PostFileWithRequest(new QueueImageToText(), | ||
new UploadFile("image", fsImage, "image")); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
```csharp | ||
var response = client.PostFilesWithRequest(new QueueImageUpscale(), | ||
[new UploadFile("image", File.OpenRead("files/test_image.jpg"), "image")] | ||
); | ||
using var fsImage = File.OpenRead("files/test_image.jpg"); | ||
var response = client.PostFileWithRequest(new QueueImageUpscale(), | ||
new UploadFile("image", fsImage, "image")); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,11 @@ | ||
```csharp | ||
using var fsImage = File.OpenRead("files/comfyui_upload_test.png"); | ||
using var fsMask = File.OpenRead("files/comfyui_upload_test_mask.png"); | ||
var response = client.PostFilesWithRequest(new QueueImageWithMask { | ||
PositivePrompt = "A beautiful sunset over the ocean", | ||
NegativePrompt = "A pixelated, low-quality image" | ||
}, | ||
[new UploadFile("image", File.OpenRead("files/comfyui_upload_test.png"), "image"), | ||
new UploadFile("mask", File.OpenRead("files/comfyui_upload_test_mask.png"), "mask")] | ||
); | ||
}, [ | ||
new UploadFile("image", fsImage, "image"), | ||
new UploadFile("mask", fsMask, "mask") | ||
]); | ||
``` |
17 changes: 9 additions & 8 deletions
17
MyApp/_includes/ai-server/cs/queue-openai-chat-completion-1.cs.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,21 @@ | ||
```csharp | ||
var client = GetLocalApiClient("https://localhost:5005"); | ||
var client = GetLocalApiClient(AiServerUrl); | ||
client.BearerToken = Environment.GetEnvironmentVariable("AI_SERVER_API_KEY"); | ||
|
||
var response = await client.ApiAsync(new QueueOpenAiChatCompletion { | ||
var api = await client.ApiAsync(new QueueOpenAiChatCompletion | ||
{ | ||
Request = new() | ||
{ | ||
Model = "gpt-4-turbo", | ||
Messages = new List<OpenAiMessage> | ||
{ | ||
Messages = | ||
[ | ||
new() { Role = "system", Content = "You are a helpful AI assistant." }, | ||
new() { Role = "user", Content = "How do LLMs work?" } | ||
}, | ||
], | ||
MaxTokens = 50 | ||
} | ||
}, | ||
}); | ||
response.ThrowIfError(); | ||
api.ThrowIfError(); | ||
// Response only returns the related job information | ||
Console.WriteLine($"RefId: {response.Response.RefId}, JobId: {response.Response.Id}"); | ||
Console.WriteLine($"RefId: {api.Response.RefId}, JobId: {api.Response.Id}"); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,20 @@ | ||
```csharp | ||
var response = client.PostFilesWithRequest(new QueueScaleImage { | ||
using var fsImage = File.OpenRead("files/test_image.jpg"); | ||
var response = client.PostFileWithRequest(new QueueScaleImage { | ||
Width = 1280, | ||
Height = 720, | ||
ReplyTo = "https://example.com/my/reply/endpoint" // optional | ||
}, | ||
[new UploadFile("test_image.jpg", File.OpenRead("files/test_image.jpg"), "image")] | ||
); | ||
new UploadFile("test_image.jpg", fsImage, "image")); | ||
|
||
var status = await client.GetAsync(new GetJobStatus { RefId = response.RefId }); | ||
GetArtifactGenerationStatusResponse status = new(); | ||
while (status.JobState is BackgroundJobState.Started or BackgroundJobState.Queued) | ||
{ | ||
status = await client.GetAsync(new GetArtifactGenerationStatus { RefId = response.RefId }); | ||
await Task.Delay(1000); | ||
status = await client.GetAsync(new GetJobStatus { RefId = response.RefId }); | ||
} | ||
|
||
// Download the scaled video | ||
var videoUrl = status.Outputs[0].Url; | ||
var videoUrl = status.Results[0].Url; | ||
videoUrl.DownloadFileTo($"scaled-image-{status.RefId}.jpg"); | ||
``` |
Oops, something went wrong.