Skip to content

Commit

Permalink
feat: allow setting X-Base-URL header (unkeyed#1870)
Browse files Browse the repository at this point in the history
* feat: allow setting X-Base-URL header

* feat: allow setting X-Base-URL header

* fix(docs): correct X-Min-Similarity value to 0.92 in settings.mdx
  • Loading branch information
chronark authored Jul 6, 2024
1 parent 7ff939a commit 318e82c
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 22 deletions.
4 changes: 2 additions & 2 deletions apps/docs/mint.json
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,8 @@
"group": "Semantic cache",
"pages": [
"semantic-cache/introduction",
"semantic-cache/similarity-threshold",
"semantic-cache/why-semantic-caching"
"semantic-cache/why-semantic-caching",
"semantic-cache/settings"
]
},
{
Expand Down
41 changes: 41 additions & 0 deletions apps/docs/semantic-cache/settings.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
---
title: Settings
description: Configure the behavior of the semantic cache
---


## Customizing the base URL

By default, the semantic cache will forward requests to the OpenAI API at `https://api.openai.com/v1`.

You can customize this, if you want to use a different LLM API or have a different base URL.

To do so, set the `X-Base-Url` header when sending requests to the cache:

```
const openai = new OpenAI({
apiKey: process.env.OPENAI_API_KEY,
baseURL: "https://<gateway>.llm.unkey.io",
defaultHeaders: {
'X-Base-Url': 'https://api.openai.com/v2'
}
});
```

## Similarity threshold

By default, the semantic cache will return a HIT if a previous response is found with a similarity score of 0.9 or above.

You can customize this, if you want to increase cache hit ratio and/or have a higher standard for returning cached responses.

To do so, set the `X-Min-Similarity` header when sending requests to the cache:

```
const openai = new OpenAI({
apiKey: process.env.OPENAI_API_KEY,
baseURL: "https://<gateway>.llm.unkey.io",
defaultHeaders: {
'X-Min-Similarity': 0.92
}
});
```
20 changes: 0 additions & 20 deletions apps/docs/semantic-cache/similarity-threshold.mdx

This file was deleted.

1 change: 1 addition & 0 deletions apps/semantic-cache/src/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ app.all("*", async (c) => {
const apiKey = bearer.replace("Bearer ", "");
const openai = new OpenAI({
apiKey,
baseURL: c.req.header("X-Base-Url"),
});
const request = (await c.req.json()) as OpenAI.Chat.Completions.ChatCompletionCreateParams;
const { db, analytics } = c.get("services");
Expand Down

0 comments on commit 318e82c

Please sign in to comment.