Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AI Tutorial #8297

Merged
merged 8 commits into from
Oct 18, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
187 changes: 187 additions & 0 deletions website/docs/feature-flag-tutorials/use-cases/ai.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
---
title: How to use feature flags with AI
slug: /feature-flag-tutorials/use-cases/ai
---

Many tech companies today are implementing some form of generative AI with LLMs into their products. This is usually a chatbot or a content generator.
alvinometric marked this conversation as resolved.
Show resolved Hide resolved

The main way to interact with LLMs today is via a set of APIs, usually either OpenAI, Anthropic or aggregators like Groq. All the APIs usually have similar parameters, like:
alvinometric marked this conversation as resolved.
Show resolved Hide resolved

- `model`: The model and the specific version of the model to use.
- `prompt`: The prompt to give to the LLM.

Because all these models vary a lot in their capabilities, chances are you'll be testing multiple models, and multiple versions of the same model, and multiple prompts.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Because all these models vary a lot in their capabilities, chances are you'll be testing multiple models, and multiple versions of the same model, and multiple prompts.
Given the wide range of capabilities across different models, it's recommended to test multiple models, versions, and prompts.


This is where feature flags are super useful. And this is what we'll be doing in this tutorial.
alvinometric marked this conversation as resolved.
Show resolved Hide resolved

We'll be starting with a basic chatbot. I know building a chatbot is a total cliché at this point, but it's a small, self-contained example that everyone knows how to interact with. You can take this example and apply it to any other use case where you need to interact with an LLM via an API.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should avoid saying "I" if possible. Also feels a bit too informal to me, how about something like:

Although building a chatbot is a common example, it’s a small and self-contained way to demonstrate these concepts.

alvinometric marked this conversation as resolved.
Show resolved Hide resolved

First, clone the repo:

```sh
git clone https://github.com/alvinometric/feature-flags-ai
cd feature-flags-ai
```

Install the dependencies
alvinometric marked this conversation as resolved.
Show resolved Hide resolved

```sh
npm install
```

Run the app
alvinometric marked this conversation as resolved.
Show resolved Hide resolved

```sh
npm run dev
```

And you should see something like this:
alvinometric marked this conversation as resolved.
Show resolved Hide resolved

![chatbot UI](./sveltekit-chatbot.png)

This is a simple Sveltekit app with a chatbot UI. It doesn't really matter if you've never used Sveltekit before. It's similar to Next.js or Nuxt or SolidStart, where your file structure defines your routes.
alvinometric marked this conversation as resolved.
Show resolved Hide resolved

The most important file for this tutorial is the `src/routes/api/chat/+server.js` file.

It creates an API endpoint at `/api/chat`. When your frontend sends a POST request to `/api/chat`, this is the code that handles it.
alvinometric marked this conversation as resolved.
Show resolved Hide resolved

```javascript
import { createOpenAI } from "@ai-sdk/openai";
import { streamText } from "ai";
import { env } from "$env/dynamic/private";

const openai = createOpenAI({
apiKey: env.OPENAI_API_KEY ?? "",
});

const variants = {
variant1: {
model: "gpt-3.5-turbo",
max_tokens: 4096,
temperature: 1,
},
variant2: {
model: "gpt-4-turbo",
max_tokens: 4096,
temperature: 1,
},
variant3: {
model: "gpt-4-vision-preview",
max_tokens: 4096,
temperature: 1,
},
};

export const POST = async ({ request }) => {
const { messages } = await request.json();
const variant = variants["variant1"];
const result = await streamText({
model: openai(variant.model),
messages,
max_tokens: variant.max_tokens,
temperature: variant.temperature,
});
return result.toDataStreamResponse();
};
```

This file is doing a few key things:

1. It sets up our OpenAI client with an API key.
alvinometric marked this conversation as resolved.
Show resolved Hide resolved
2. It defines different AI model configurations in the `variants` object.
alvinometric marked this conversation as resolved.
Show resolved Hide resolved
3. The `POST` function handles incoming chat requests.
alvinometric marked this conversation as resolved.
Show resolved Hide resolved

When a request comes in, it:

- Extracts the messages from the request body
alvinometric marked this conversation as resolved.
Show resolved Hide resolved
- Selects a variant (currently hardcoded to 'variant1')
alvinometric marked this conversation as resolved.
Show resolved Hide resolved
- Uses the OpenAI API to generate a response
alvinometric marked this conversation as resolved.
Show resolved Hide resolved
- Streams the response back to the client
alvinometric marked this conversation as resolved.
Show resolved Hide resolved

The `streamText` function part of some utilities provided by Vercel's AI SDK, which helps deal with real-time streaming of the AI's responses and other AI-related tasks.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This sentence is maybe missing a word?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe: The streamText function, part of Vercel’s AI SDK, assists with the real-time streaming of AI responses and other AI-related tasks.


## Creating a feature flag with AI variants

Instead of hardcoding `variant1`, we want to use feature flags to dynamically choose which AI model to use. This will let us easily switch between models, test different configurations, or even do some A/B testing.

To implement this, we'll need to:

1. Set up a feature flag provider (we'll use Unleash).
2. Replace our static `variants` selection with feature flag calls.
alvinometric marked this conversation as resolved.
Show resolved Hide resolved
3. Use the feature flag in our code to determine which AI model and settings to use for each request.

### Install a local feature flag provider

In this section, we’ll install Unleash, run the instance locally, log in, and create a feature flag. If you prefer, you can use other tools instead of Unleash, but you’ll need to update the code accordingly. The basic steps will probably be the same.
alvinometric marked this conversation as resolved.
Show resolved Hide resolved

Use Git to clone the Unleash repository and Docker to build and run it. Open a terminal window and run the following commands:

```sh
git clone https://github.com/unleash/unleash.git
cd unleash
docker compose up -d
```

You will now have Unleash installed onto your machine and running in the background. You can access this instance in your web browser at [http://localhost:4242](http://localhost:4242).
alvinometric marked this conversation as resolved.
Show resolved Hide resolved

Log in to the platform using these credentials:

```
Username: admin
Password: unleash4all
```

Click the ‘New feature flag’ button to create a new feature flag.
alvinometric marked this conversation as resolved.
Show resolved Hide resolved

<!-- ![Create a new feature flag](./new-ff.png) -->

## Querying AI feature flags

Now let's go back to the code and grab our AI config from the feature flag that we just created.

First, install the Unleash Node.js client:

```sh
npm install unleash-client
```

Now, let's modify our `+server.js` file to use Unleash:

```javascript
import { initialize } from "unleash-client";
import { createOpenAI } from "@ai-sdk/openai";
import { streamText } from "ai";
import { env } from "$env/dynamic/private";

const openai = createOpenAI({
apiKey: env.OPENAI_API_KEY ?? "",
});

const unleash = initialize({
url: "http://localhost:4242/api/",
appName: "my-ai-app",
customHeaders: { Authorization: env.UNLEASH_API_KEY ?? "" },
});

export const POST = async ({ request }) => {
const { messages } = await request.json();

// Get the feature flag variant
const variant = unleash.getVariant("gpt-versions");
alvinometric marked this conversation as resolved.
Show resolved Hide resolved

const result = await streamText({
model: openai(variant.model),
messages,
max_tokens: variant.max_tokens,
temperature: variant.temperature,
});

return result.toDataStreamResponse();
};
```

This setup uses the Unleash client to fetch the value of a feature flag called `gpt-versions`.
alvinometric marked this conversation as resolved.
Show resolved Hide resolved

Now, instead of hardcoding 'variant1', we're dynamically choosing the AI model based on the feature flag variant.

This setup gives us a ton of flexibility. Want to roll out GPT-4 to 10% of your users? Easy. Need to quickly switch everyone back to GPT-3.5 because of a bug? No problem. You can do all of this from your Unleash dashboard without touching your code.
alvinometric marked this conversation as resolved.
Show resolved Hide resolved
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions website/sidebars.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,11 @@ module.exports = {
label: 'Gradual Rollout',
id: 'feature-flag-tutorials/use-cases/gradual-rollout',
},
{
type: 'doc',
label: 'Feature flags for AI',
alvinometric marked this conversation as resolved.
Show resolved Hide resolved
id: 'feature-flag-tutorials/use-cases/ai',
},
],
},
{
Expand Down
Loading