From 8b9cdeda6aeb74e9ef36bb4cc1b4e7a70cad14a6 Mon Sep 17 00:00:00 2001 From: Darren Reid Date: Wed, 2 Oct 2024 17:02:33 +1000 Subject: [PATCH] More AI server docs. --- .../_pages/ai-server/usage/audio-endpoints.md | 0 MyApp/_pages/ai-server/usage/chat.md | 35 ++++++++-- .../_pages/ai-server/usage/image-endpoints.md | 70 +++++++++++++++++++ MyApp/_pages/ai-server/usage/text-to-image.md | 58 +++++++++++++++ 4 files changed, 156 insertions(+), 7 deletions(-) create mode 100644 MyApp/_pages/ai-server/usage/audio-endpoints.md create mode 100644 MyApp/_pages/ai-server/usage/image-endpoints.md create mode 100644 MyApp/_pages/ai-server/usage/text-to-image.md diff --git a/MyApp/_pages/ai-server/usage/audio-endpoints.md b/MyApp/_pages/ai-server/usage/audio-endpoints.md new file mode 100644 index 0000000000..e69de29bb2 diff --git a/MyApp/_pages/ai-server/usage/chat.md b/MyApp/_pages/ai-server/usage/chat.md index 0faa552c98..9ac15614de 100644 --- a/MyApp/_pages/ai-server/usage/chat.md +++ b/MyApp/_pages/ai-server/usage/chat.md @@ -20,10 +20,10 @@ var response = client.Post(new CreateOpenAiChat { }, MaxTokens = 50 }, - RefId = "", - Provider = "openai", - ReplyTo = "https://myapp.com/reply", - Sync = true + RefId = "", // Optional + Provider = "openai", // Optional + ReplyTo = "https://myapp.com/reply", // Optional + Sync = true // Optional }); ``` @@ -33,7 +33,7 @@ Additional optional features on the request to enhance the usage of AI Server in - **Provider**: Force the request to use a specific provider, overriding the selection logic. - **ReplyTo**: A HTTP URL to send the response to on completion of the request. - **Tag**: A tag to help categorize the request for easier tracking. -- **Sync**: A boolean value to determine if the request should be processed synchronously (default is asynchronous returning job info). +- **Sync**: A boolean value to determine if the request should be processed synchronously (default is `false`). These additional request properties are consistent across all AI Server endpoints, providing a common interface for all AI services. @@ -42,7 +42,28 @@ These additional request properties are consistent across all AI Server endpoint 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. 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. -::include ai-server/cs/open-ai-requests-1.cs.md:: +```csharp +// Using OpenAI API directly via JsonApiClient +var client = new JsonApiClient("https://api.openai.com/v1"); +client.AddHeader("Authorization", "Bearer " + Environment.GetEnvironmentVariable("OPENAI_API_KEY")); + +// Using AI Server DTOs with OpenAI API +var request = new OpenAiChat { + Model = "gpt-4-turbo", + Messages = new List { + new OpenAiMessage { Role = "system", Content = "You are a helpful AI assistant." }, + new OpenAiMessage { Role = "user", Content = "How do LLMs work?" } + }, + MaxTokens = 50 +}; + +// Typed response DTO +var response = await client.PostAsync("/chat/completions", request); +``` + +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. + + + -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. diff --git a/MyApp/_pages/ai-server/usage/image-endpoints.md b/MyApp/_pages/ai-server/usage/image-endpoints.md new file mode 100644 index 0000000000..9b110b7b8d --- /dev/null +++ b/MyApp/_pages/ai-server/usage/image-endpoints.md @@ -0,0 +1,70 @@ +--- +title: "Image to Image" +description: "Generating images from images with AI Server" +--- + +# Image to Image + +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. + +## Other Image to Image Tasks + +In addition to just Image to Image, AI Server also supports: + +- **ImageWithMask**: This task takes an image and a mask as input and uses the provided prompts only for the matching masked area. +- **ImageUpscale**: This task takes an image and upscales it to a higher resolution using the provided prompts (2x only currently). +- **ImageToText**: This task takes an image and generates text based on the image content. + +## Using Image to Image + +To generate an image from an image, you can use the `ImageToImage` request: + +```csharp +var request = new ImageToImage() +{ + PositivePrompt = "A beautiful sunset over the ocean", + NegativePrompt = "A pixelated, low-quality image", + Sync = true +}; + +var response = client.PostFilesWithRequest( + request, + [new UploadFile("image", File.OpenRead("sunset.jpg"), "sunset.jpg")] +); +response.Outputs[0].Url.DownloadFileTo("ocean-sunset.webp"); +``` + +A similar pattern can be used for the `ImageWithMask` request: + +```csharp +var request = new ImageWithMask() +{ + PositivePrompt = "A beautiful sunset over the ocean", + NegativePrompt = "A pixelated, low-quality image", + Sync = true +}; + +var response = client.PostFilesWithRequest( + request, + [new UploadFile("image", File.OpenRead("sunset.jpg"), "sunset.jpg"), + new UploadFile("mask", File.OpenRead("mask.jpg"), "mask.jpg")] +); +response.Outputs[0].Url.DownloadFileTo("ocean-sunset.webp"); +``` + +The `ImageUpscale` request is similar, but only requires the image file: + +```csharp +var request = new ImageUpscale() +{ + Sync = true +}; + +var response = client.PostFilesWithRequest( + request, + [new UploadFile("image", File.OpenRead("low-res.jpg"), "low-res.jpg")] +); +response.Outputs[0].Url.DownloadFileTo("high-res.webp"); +``` + +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. \ No newline at end of file diff --git a/MyApp/_pages/ai-server/usage/text-to-image.md b/MyApp/_pages/ai-server/usage/text-to-image.md new file mode 100644 index 0000000000..ee22d39d0a --- /dev/null +++ b/MyApp/_pages/ai-server/usage/text-to-image.md @@ -0,0 +1,58 @@ +--- +title: "Text to Image" +description: "Generating images from text with AI Server" +--- + +# Text to Image + +As well as interacting with LLMs to generate text, AI Server can also generate images from text from a few different providers. + +- **DALL-E** +- **Replicate API** +- **Comfy UI** + +## DALL-E + +When you configure AI Server with an OpenAI API key, you can use the DALL-E model to generate images from text. + +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. + +## Replicate API + +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. + +Replicate provides access to the Flux family of models, which can generate images from text using: + +- **Flux Schnell** +- **Flux Dev** +- **Flux Pro** + +## Comfy UI + +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). + +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. + +## Using Text to Image + +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. + +```csharp +var request = new TextToImage() +{ + Height = 768, + Width = 768, + Model = "flux-schnell", + PositivePrompt = "A happy llama", + NegativePrompt = "bad quality, blurry image", + Sync = true +}; + +var response = await client.PostAsync(request); +response.Outputs[0].Url.DownloadFileTo("llama.webp"); +``` + +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. + +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. +