Skip to content

Commit 8b9cded

Browse files
committed
More AI server docs.
1 parent ddea290 commit 8b9cded

File tree

4 files changed

+156
-7
lines changed

4 files changed

+156
-7
lines changed

MyApp/_pages/ai-server/usage/audio-endpoints.md

Whitespace-only changes.

MyApp/_pages/ai-server/usage/chat.md

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ var response = client.Post(new CreateOpenAiChat {
2020
},
2121
MaxTokens = 50
2222
},
23-
RefId = "<unique-id>",
24-
Provider = "openai",
25-
ReplyTo = "https://myapp.com/reply",
26-
Sync = true
23+
RefId = "<unique-id>", // Optional
24+
Provider = "openai", // Optional
25+
ReplyTo = "https://myapp.com/reply", // Optional
26+
Sync = true // Optional
2727
});
2828
```
2929

@@ -33,7 +33,7 @@ Additional optional features on the request to enhance the usage of AI Server in
3333
- **Provider**: Force the request to use a specific provider, overriding the selection logic.
3434
- **ReplyTo**: A HTTP URL to send the response to on completion of the request.
3535
- **Tag**: A tag to help categorize the request for easier tracking.
36-
- **Sync**: A boolean value to determine if the request should be processed synchronously (default is asynchronous returning job info).
36+
- **Sync**: A boolean value to determine if the request should be processed synchronously (default is `false`).
3737

3838
These additional request properties are consistent across all AI Server endpoints, providing a common interface for all AI services.
3939

@@ -42,7 +42,28 @@ These additional request properties are consistent across all AI Server endpoint
4242
One advantage of using AI Server is that it provides a common set of request DTOs in 11 different languages that are compatible with OpenAI's API. This allows you to switch between OpenAI and AI Server without changing your client code.
4343
This means you can switch to using typed APIs in your preferred language with your existing service providers OpenAI compatible APIs, and optionally switch to AI Server when you're ready to self-host your AI services for better value.
4444

45-
::include ai-server/cs/open-ai-requests-1.cs.md::
45+
```csharp
46+
// Using OpenAI API directly via JsonApiClient
47+
var client = new JsonApiClient("https://api.openai.com/v1");
48+
client.AddHeader("Authorization", "Bearer " + Environment.GetEnvironmentVariable("OPENAI_API_KEY"));
49+
50+
// Using AI Server DTOs with OpenAI API
51+
var request = new OpenAiChat {
52+
Model = "gpt-4-turbo",
53+
Messages = new List<OpenAiMessage> {
54+
new OpenAiMessage { Role = "system", Content = "You are a helpful AI assistant." },
55+
new OpenAiMessage { Role = "user", Content = "How do LLMs work?" }
56+
},
57+
MaxTokens = 50
58+
};
59+
60+
// Typed response DTO
61+
var response = await client.PostAsync<OpenAiChatResponse>("/chat/completions", request);
62+
```
63+
64+
This shows usage of the `OpenAiChat` request DTO directly with OpenAI's API using the ServiceStack `JsonApiClient`, so you get the benefits of using typed APIs in your preferred language with your existing service providers OpenAI compatible APIs.
65+
66+
67+
4668

47-
This shows usage of the `OpenAiChat` request DTO directly with OpenAI's API. The same request DTO can be used with AI Server's `/api/CreateOpenAiChat` endpoint, populated the same way.
4869

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
---
2+
title: "Image to Image"
3+
description: "Generating images from images with AI Server"
4+
---
5+
6+
# Image to Image
7+
8+
AI Server has built-in ComfyUI workflows for doing image-to-image generation tasks like inpainting. This takes an image as input and generates a new image based on the input image and any additional prompts you provide.
9+
10+
## Other Image to Image Tasks
11+
12+
In addition to just Image to Image, AI Server also supports:
13+
14+
- **ImageWithMask**: This task takes an image and a mask as input and uses the provided prompts only for the matching masked area.
15+
- **ImageUpscale**: This task takes an image and upscales it to a higher resolution using the provided prompts (2x only currently).
16+
- **ImageToText**: This task takes an image and generates text based on the image content.
17+
18+
## Using Image to Image
19+
20+
To generate an image from an image, you can use the `ImageToImage` request:
21+
22+
```csharp
23+
var request = new ImageToImage()
24+
{
25+
PositivePrompt = "A beautiful sunset over the ocean",
26+
NegativePrompt = "A pixelated, low-quality image",
27+
Sync = true
28+
};
29+
30+
var response = client.PostFilesWithRequest<GenerationResponse>(
31+
request,
32+
[new UploadFile("image", File.OpenRead("sunset.jpg"), "sunset.jpg")]
33+
);
34+
response.Outputs[0].Url.DownloadFileTo("ocean-sunset.webp");
35+
```
36+
37+
A similar pattern can be used for the `ImageWithMask` request:
38+
39+
```csharp
40+
var request = new ImageWithMask()
41+
{
42+
PositivePrompt = "A beautiful sunset over the ocean",
43+
NegativePrompt = "A pixelated, low-quality image",
44+
Sync = true
45+
};
46+
47+
var response = client.PostFilesWithRequest<GenerationResponse>(
48+
request,
49+
[new UploadFile("image", File.OpenRead("sunset.jpg"), "sunset.jpg"),
50+
new UploadFile("mask", File.OpenRead("mask.jpg"), "mask.jpg")]
51+
);
52+
response.Outputs[0].Url.DownloadFileTo("ocean-sunset.webp");
53+
```
54+
55+
The `ImageUpscale` request is similar, but only requires the image file:
56+
57+
```csharp
58+
var request = new ImageUpscale()
59+
{
60+
Sync = true
61+
};
62+
63+
var response = client.PostFilesWithRequest<GenerationResponse>(
64+
request,
65+
[new UploadFile("image", File.OpenRead("low-res.jpg"), "low-res.jpg")]
66+
);
67+
response.Outputs[0].Url.DownloadFileTo("high-res.webp");
68+
```
69+
70+
Upscaling is currently limited to 2x the original resolution, and it requires ComfyUI has downloaded the necessary model "RealESRGAN_x2.pth" to perform the upscale.
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
---
2+
title: "Text to Image"
3+
description: "Generating images from text with AI Server"
4+
---
5+
6+
# Text to Image
7+
8+
As well as interacting with LLMs to generate text, AI Server can also generate images from text from a few different providers.
9+
10+
- **DALL-E**
11+
- **Replicate API**
12+
- **Comfy UI**
13+
14+
## DALL-E
15+
16+
When you configure AI Server with an OpenAI API key, you can use the DALL-E model to generate images from text.
17+
18+
You can control these providers via the Admin Dashboard in AI Server if you want to enable or disable them on a per model basis.
19+
20+
## Replicate API
21+
22+
Replicate is another provider that can generate images from text. You can configure Replicate in the Admin Dashboard in AI Server or include the `REPLICATE_API_KEY` in your `.env` file during the first run of AI Server.
23+
24+
Replicate provides access to the Flux family of models, which can generate images from text using:
25+
26+
- **Flux Schnell**
27+
- **Flux Dev**
28+
- **Flux Pro**
29+
30+
## Comfy UI
31+
32+
Comfy UI is a self-hosted agent that can process image requests and other modalities. You can configure Comfy UI in the Admin Dashboard in AI Server after you have set a ComfyUI instance using [the related ComfyUI Extension](https://github.com/ServiceStack/agent-comfy).
33+
34+
When configuring the Comfy AI Provider, you can provide the URL of your ComfyUI instance, and any API key required to authenticate with it, and you will get a list of the models available in your ComfyUI instance to enable for the provider.
35+
36+
## Using Text to Image
37+
38+
Once you have configured your AI Server with the providers you want to use, you can make requests to the AI Server API to generate images from text.
39+
40+
```csharp
41+
var request = new TextToImage()
42+
{
43+
Height = 768,
44+
Width = 768,
45+
Model = "flux-schnell",
46+
PositivePrompt = "A happy llama",
47+
NegativePrompt = "bad quality, blurry image",
48+
Sync = true
49+
};
50+
51+
var response = await client.PostAsync(request);
52+
response.Outputs[0].Url.DownloadFileTo("llama.webp");
53+
```
54+
55+
This request will generate an image of a happy llama using the Flux Schnell model. The `PositivePrompt` and `NegativePrompt` properties are used to guide the model on what to generate, and what to avoid. The `Sync` property is used to determine if the request should be processed synchronously or asynchronously. By default, requests are processed asynchronously.
56+
57+
Flux Schnell is also available in the Comfy UI agent, so you can use the same model with multiple providers, or switch between providers without changing your client code.
58+

0 commit comments

Comments
 (0)