add llamastack support

This commit is contained in:
Kai Wu 2025-06-16 16:12:12 -07:00
parent 64151c1242
commit fc950f4cd3
5 changed files with 93 additions and 125 deletions

View file

@ -1,12 +1,12 @@
{
"name": "@continuedev/openai-adapters",
"version": "1.0.36",
"version": "1.0.38",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "@continuedev/openai-adapters",
"version": "1.0.36",
"version": "1.0.38",
"license": "Apache-2.0",
"dependencies": {
"@continuedev/config-types": "^1.0.5",

View file

@ -0,0 +1,55 @@
import { streamSse } from "@continuedev/fetch";
import { ChatCompletionChunk } from "openai/resources/index";
import { LlamastackConfig } from "../types.js";
import { chatChunk, customFetch } from "../util.js";
import { OpenAIApi } from "./OpenAI.js";
import { FimCreateParamsStreaming } from "./base.js";
export const Llamastack_API_BASE = "http://localhost:8321/v1/openai/v1/";
export class LlamastackApi extends OpenAIApi {
constructor(config: LlamastackConfig) {
super({
...config,
provider: "openai",
apiBase: config.apiBase ?? Llamastack_API_BASE,
apiKey: config.apiKey ?? 'dummy',
});
}
async *fimStream(
body: FimCreateParamsStreaming,
signal: AbortSignal,
): AsyncGenerator<ChatCompletionChunk, any, unknown> {
const endpoint = new URL("completions", this.apiBase);
const resp = await customFetch(this.config.requestOptions)(endpoint, {
method: "POST",
body: JSON.stringify({
model: body.model,
prompt: body.prompt,
suffix: body.suffix,
max_tokens: body.max_tokens,
max_completion_tokens: (body as any).max_completion_tokens,
temperature: body.temperature,
top_p: body.top_p,
frequency_penalty: body.frequency_penalty,
presence_penalty: body.presence_penalty,
stop: body.stop,
stream: true,
}),
headers: {
"Content-Type": "application/json",
Accept: "application/json",
"x-api-key": this.config.apiKey ?? "",
Authorization: `Bearer ${this.config.apiKey}`,
},
signal,
});
for await (const chunk of streamSse(resp as any)) {
yield chatChunk({
content: chunk.choices[0].text,
finish_reason: chunk.finish_reason,
model: body.model,
});
}
}
}

View file

@ -7,6 +7,7 @@ import { DeepSeekApi } from "./apis/DeepSeek.js";
import { GeminiApi } from "./apis/Gemini.js";
import { InceptionApi } from "./apis/Inception.js";
import { JinaApi } from "./apis/Jina.js";
import { LlamastackApi } from "./apis/LlamaStack.js";
import { MockApi } from "./apis/Mock.js";
import { MoonshotApi } from "./apis/Moonshot.js";
import { OpenAIApi } from "./apis/OpenAI.js";
@ -51,6 +52,8 @@ export function constructLlmApi(config: LLMConfig): BaseLlmApi | undefined {
return new InceptionApi(config);
case "watsonx":
return new WatsonXApi(config);
case "llamastack":
return new LlamastackApi(config);
case "x-ai":
return openAICompatible("https://api.x.ai/v1/", config);
case "voyage":
@ -117,9 +120,10 @@ export {
type Completion,
type CompletionCreateParams,
type CompletionCreateParamsNonStreaming,
type CompletionCreateParamsStreaming,
type CompletionCreateParamsStreaming
} from "openai/resources/index";
// export
export type { BaseLlmApi } from "./apis/base.js";
export type { LLMConfig } from "./types.js";

View file

@ -1,10 +1,8 @@
import { ModelConfig } from "@continuedev/config-yaml";
import * as dotenv from "dotenv";
import { DEEPSEEK_API_BASE } from "../apis/DeepSeek.js";
import { INCEPTION_API_BASE } from "../apis/Inception.js";
import { OpenAIApi } from "../apis/OpenAI.js";
import { constructLlmApi } from "../index.js";
import { getLlmApi, testChat, testEmbed } from "./util.js";
import { getLlmApi, testChat, testEmbed, testFim } from "./util.js";
dotenv.config();
@ -33,99 +31,30 @@ function testConfig(_config: ModelConfig & { options?: TestConfigOptions }) {
if (config.roles?.includes("embed")) {
testEmbed(api, model);
}
if (config.roles?.includes("embed")) {
testEmbed(api, model);
if (config.roles?.includes("autocomplete")) {
testFim(api, model);
}
}
const TESTS: Omit<ModelConfig & { options?: TestConfigOptions }, "name">[] = [
{
provider: "openai",
model: "gpt-4o",
apiKey: process.env.OPENAI_API_KEY!,
roles: ["chat"],
{
provider: "llamastack",
model: "meta-llama/Llama-4-Maverick-17B-128E-Instruct-FP8",
apiBase: "http://localhost:8321/v1/openai/v1/",
roles: ["apply", "chat", "edit"],
options: { skipTools: false },
},
{
provider: "anthropic",
model: "claude-3-5-haiku-latest",
apiKey: process.env.ANTHROPIC_API_KEY!,
roles: ["chat"],
},
{
provider: "gemini",
model: "gemini-1.5-flash-latest",
apiKey: process.env.GEMINI_API_KEY!,
roles: ["chat"],
},
{
provider: "mistral",
model: "codestral-latest",
apiKey: process.env.MISTRAL_API_KEY!,
apiBase: "https://api.mistral.ai/v1",
roles: ["autocomplete"],
},
{
provider: "deepseek",
model: "deepseek-coder",
apiKey: process.env.DEEPSEEK_API_KEY!,
roles: ["autocomplete"],
},
{
provider: "openai",
model: "text-embedding-3-small",
apiKey: process.env.OPENAI_API_KEY!,
provider: "llamastack",
model: "all-MiniLM-L6-v2",
apiBase: "http://localhost:8321/v1/openai/v1/",
roles: ["embed"],
},
{
provider: "voyage",
model: "voyage-code-3",
apiKey: process.env.VOYAGE_API_KEY!,
roles: ["embed"],
},
// {
// provider: "cohere",
// model: "embed-v4.0",
// apiKey: process.env.COHERE_API_KEY!,
// roles: ["embed"],
// },
// {
// provider: "gemini",
// model: "models/text-embedding-004",
// apiKey: process.env.GEMINI_API_KEY!,
// roles: ["embed"],
// },
{
provider: "voyage",
model: "rerank-lite-1",
apiKey: process.env.VOYAGE_API_KEY!,
roles: ["rerank"],
},
// {
// provider: "cohere",
// model: "rerank-v3.5",
// apiKey: process.env.COHERE_API_KEY!,
// roles: ["rerank"],
// },
{
provider: "azure",
model: "gpt-4.1",
apiBase: "https://continue-openai.openai.azure.com",
apiKey: process.env.AZURE_OPENAI_GPT41_API_KEY,
env: {
deployment: "gpt-4.1",
apiVersion: "2024-02-15-preview",
apiType: "azure-openai",
},
roles: ["chat"],
},
{
provider: "azure",
model: "mistral-small-2503",
apiBase: "https://nate-0276-resource.services.ai.azure.com/models",
apiKey: process.env.AZURE_FOUNDRY_MISTRAL_SMALL_API_KEY,
roles: ["chat"],
options: { skipTools: true },
{
provider: "llamastack",
model: "qwen2.5-coder:1.5b",
apiBase: "http://localhost:8321/v1/openai/v1/",
roles: ["autocomplete"],
},
];
@ -136,41 +65,14 @@ TESTS.forEach((config) => {
});
describe("Configuration", () => {
it("should configure DeepSeek OpenAI client with correct apiBase and apiKey", () => {
const deepseek = constructLlmApi({
provider: "deepseek",
it("should configure OpenAI client with correct apiBase and apiKey", () => {
const openai = constructLlmApi({
provider: "openai",
apiKey: "sk-xxx",
apiBase: "http://localhost:8321/v1/openai/v1/",
});
expect((deepseek as OpenAIApi).openai.baseURL).toBe(DEEPSEEK_API_BASE);
expect((deepseek as OpenAIApi).openai.apiKey).toBe("sk-xxx");
const deepseek2 = constructLlmApi({
provider: "deepseek",
apiKey: "sk-xxx",
apiBase: "https://api.example.com",
});
expect((deepseek2 as OpenAIApi).openai.baseURL).toBe(
"https://api.example.com",
);
});
it("should configure Inception OpenAI client with correct apiBase and apiKey", () => {
const inception = constructLlmApi({
provider: "inception",
apiKey: "sk-xxx",
});
expect((inception as OpenAIApi).openai.baseURL).toBe(INCEPTION_API_BASE);
expect((inception as OpenAIApi).openai.apiKey).toBe("sk-xxx");
const inception2 = constructLlmApi({
provider: "inception",
apiKey: "sk-xxx",
apiBase: "https://api.example.com",
});
expect((inception2 as OpenAIApi).openai.baseURL).toBe(
"https://api.example.com",
);
expect((openai as OpenAIApi).openai.baseURL).toBe("http://localhost:8321/v1/openai/v1/");
expect((openai as OpenAIApi).openai.apiKey).toBe("sk-xxx");
});
});

View file

@ -71,9 +71,15 @@ export const DeepseekConfigSchema = OpenAIConfigSchema.extend({
});
export type DeepseekConfig = z.infer<typeof DeepseekConfigSchema>;
export const LlamastackConfigSchema = OpenAIConfigSchema.extend({
provider: z.literal("llamastack"),
});
export type LlamastackConfig = z.infer<typeof LlamastackConfigSchema>;
export const MockConfigSchema = BasePlusConfig.extend({
provider: z.literal("mock"),
});
export type MockConfig = z.infer<typeof MockConfigSchema>;
// Other APIs
@ -146,5 +152,6 @@ export const LLMConfigSchema = z.discriminatedUnion("provider", [
JinaConfigSchema,
MockConfigSchema,
InceptionConfigSchema,
LlamastackConfigSchema,
]);
export type LLMConfig = z.infer<typeof LLMConfigSchema>;