Skip to content

Commit

Permalink
ai-server docs WIP, adding usage example code includes to match ai-se…
Browse files Browse the repository at this point in the history
…rver-examples repository.
  • Loading branch information
Layoric committed Oct 1, 2024
1 parent 3378f98 commit f3d0d98
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 1 deletion.
15 changes: 15 additions & 0 deletions MyApp/_includes/ai-server/cs/open-ai-requests-1.cs.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
```csharp
// Using AI Server DTOs with OpenAI API
var request = new OpenAiChat {
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 json = JsonSerializer.SerializeToString(request);
var response = await client.PostAsync("https://api.openai.com/v1/chat/completions",
new StringContent(json, Encoding.UTF8, "application/json"));
```
4 changes: 3 additions & 1 deletion MyApp/_pages/ai-server/install/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,6 @@ docker compose up
Once the AI Server is running, you can access the Admin Portal at [http://localhost:5005/admin](http://localhost:5005/admin) to configure your AI providers and generate API keys.
If you first ran the AI Server with configured API Keys in your `.env` file, you providers will be automatically configured for the related services.

> You can reset the process by deleting your local `App_Data` directory and rerunning `docker compose up`.
> You can reset the process by deleting your local `App_Data` directory and rerunning `docker compose up`.
You will then be able to make requests to the AI Server API endpoints, and access the Admin Portal user interface like the [Chat interface](http://localhost:5005/admin/Chat) to use your AI Provider models.
48 changes: 48 additions & 0 deletions MyApp/_pages/ai-server/usage/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
---
title: AI Server API Usage
---

# AI Server API Usage

AI Server provides a unified API to process requests for AI services to access LLMs, Image Generation, Transcription, and more. The API is designed to be simple to use and easy to integrate into your applications providing many supported languages and frameworks.

## Making a Chat Request

To make a chat request to AI Server, you can use the `/api/CreateOpenAiChat` endpoint. This endpoint requires a `CreateOpenAiChat` request DTO that contains a property matching the `OpenAI` API.

```csharp
var response = client.Post(new CreateOpenAiChat {
Request = new OpenAiChat {
Model = "gpt-3.5-turbo",
Messages = new List<OpenAiMessage> {
new OpenAiMessage { Role = "system", Content = "You are a helpful AI assistant." },
new OpenAiMessage { Role = "user", Content = "How do I ..." }
}
}
});
```

Additional optional features on the request to enhance the usage of AI Server include:

- **RefId**: A unique identifier for the request specified by the client to more easily track the progress of the request.
- **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.

## Using the AI Server Request DTOs with other OpenAI compatible APIs

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.

```csharp
// Using OpenAI API
var request = new OpenAiChat {
Model = "gpt-3.5-turbo",
Messages = new List<OpenAiMessage> {
new OpenAiMessage { Role = "system", Content = "You are a helpful AI assistant." },
new OpenAiMessage { Role = "user", Content = "How do I ..." }
}
};

var response = await client.PostAsync("https://api.openai.com/v1/", request);
```

0 comments on commit f3d0d98

Please sign in to comment.