Query vLLM OpenAI /models endpoint to get model name and context window (#1632)

* add autodetecting context length for OpenAI-like custom deployments like vLLM

* add new subclass vllm from OpenAI

* revert whitespace change

* add documentation

---------

Co-authored-by: Simon Müller <simon.mueller@xitaso.com>
This commit is contained in:
Simon 2024-08-06 01:23:33 +02:00 • committed by GitHub
parent a7fb390ac0
commit 9e6461e807
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 64 additions and 8 deletions

View file

@ -104,13 +104,13 @@ class OpenAI extends BaseLLM {
this.maxStopWords !== undefined
? options.stop?.slice(0, this.maxStopWords)
: url.host === "api.deepseek.com"
? options.stop?.slice(0, 16)
: url.port === "1337" ||
url.host === "api.openai.com" ||
url.host === "api.groq.com" ||
this.apiType === "azure"
? options.stop?.slice(0, 4)
: options.stop,
? options.stop?.slice(0, 16)
: url.port === "1337" ||
url.host === "api.openai.com" ||
url.host === "api.groq.com" ||
this.apiType === "azure"
? options.stop?.slice(0, 4)
: options.stop,
};
return finalOptions;
@ -139,7 +139,7 @@ class OpenAI extends BaseLLM {
return completion;
}
private _getEndpoint(
protected _getEndpoint(
endpoint: "chat/completions" | "completions" | "models",
) {
if (this.apiType === "azure") {

30
core/llm/llms/Vllm.ts Normal file
View file

@ -0,0 +1,30 @@
import { LLMOptions, ModelProvider } from "../../index.js";
import OpenAI from "./OpenAI.js";
class Vllm extends OpenAI {
constructor(options: LLMOptions) {
super(options);
this._setupCompletionOptions();
}
private _setupCompletionOptions() {
this.fetch(this._getEndpoint("models"), {
method: "GET",
headers: this._getHeaders(),
}).then(async (response) => {
if (response.status !== 200) {
console.warn(
"Error calling vLLM /models endpoint: ",
await response.text(),
);
return;
}
const json = await response.json();
const data = json.data;
this.model = data.id;
this.contextLength = Number.parseInt(data.max_model_len);
});
}
}
export default Vllm;

View file

@ -12,6 +12,7 @@ OpenAI compatible servers
- [LocalAI](https://localai.io/basics/getting_started/)
- [llama-cpp-python](https://github.com/abetlen/llama-cpp-python#web-server)
- [TensorRT-LLM](https://github.com/NVIDIA/trt-llm-as-openai-windows?tab=readme-ov-file#examples)
- [vLLM](https://docs.vllm.ai/en/latest/serving/openai_compatible_server.html) [^1]
OpenAI compatible APIs
@ -40,4 +41,7 @@ To force usage of `chat/completions` instead of `completions` endpoint you can s
"useLegacyCompletionsEndpoint": false
```
[^1]: Use the [Vllm Model Provider](./vllm.md) instead.
[View the source](https://github.com/continuedev/continue/blob/main/core/llm/llms/OpenAI.ts)

View file

@ -0,0 +1,22 @@
# vLLM
Run the OpenAI-compatible server by vLLM using `vllm serve`. See their [server documentation](https://docs.vllm.ai/en/latest/serving/openai_compatible_server.html) and the [engine arguments documentation](https://docs.vllm.ai/en/latest/models/engine_args.html).
```shell
vllm serve NousResearch/Meta-Llama-3-8B-Instruct --max-model-len 1024
```
The continue implementation uses [OpenAI](./openai.md) under the hood and automatically selects the available model. You only need to set the `apiBase` like this:
```json title="~/.continue/config.json"
{
"models": [
{
"title": "My vLLM OpenAI-compatible server",
"apiBase": "http://localhost:8000/v1"
}
]
}
```
[View the source](https://github.com/continuedev/continue/blob/main/core/llm/llms/Vllm.ts)