append path to url if not present
This commit is contained in:
parent
648899034f
commit
c84f2a1af9
3 changed files with 61 additions and 32 deletions
|
|
@ -18,6 +18,7 @@ import { VertexAIApi } from "./apis/VertexAI.js";
|
|||
import { WatsonXApi } from "./apis/WatsonX.js";
|
||||
import { BaseLlmApi } from "./apis/base.js";
|
||||
import { LLMConfig, OpenAIConfigSchema } from "./types.js";
|
||||
import { appendPathToUrlIfNotPresent } from "./util/appendPathToUrl.js";
|
||||
|
||||
dotenv.config();
|
||||
|
||||
|
|
@ -143,13 +144,8 @@ export function constructLlmApi(config: LLMConfig): BaseLlmApi | undefined {
|
|||
case "ollama":
|
||||
// for openai compaitability, we need to add /v1 to the end of the url
|
||||
// this is required for cli (for core, endpoints are overriden by core/llm/llms/Ollama.ts)
|
||||
if (config.apiBase && !new RegExp(/v1\/?$/).test(config.apiBase)) {
|
||||
if (config.apiBase.endsWith("/")) {
|
||||
config.apiBase += "v1/";
|
||||
} else {
|
||||
config.apiBase += "/v1/";
|
||||
}
|
||||
}
|
||||
if (config.apiBase)
|
||||
config.apiBase = appendPathToUrlIfNotPresent(config.apiBase, "v1");
|
||||
return openAICompatible("http://localhost:11434/v1/", config);
|
||||
case "mock":
|
||||
return new MockApi();
|
||||
|
|
|
|||
|
|
@ -310,35 +310,51 @@ describe("Configuration", () => {
|
|||
expect((azure as OpenAIApi).openai.apiKey).toBe("sk-xxx");
|
||||
});
|
||||
|
||||
it('should have correct default API base for "ollama"', () => {
|
||||
const ollama = constructLlmApi({
|
||||
provider: "ollama",
|
||||
describe("ollama api base", () => {
|
||||
it('should have correct default API base for "ollama"', () => {
|
||||
const ollama = constructLlmApi({
|
||||
provider: "ollama",
|
||||
});
|
||||
|
||||
expect((ollama as OpenAIApi).openai.baseURL).toBe(
|
||||
"http://localhost:11434/v1/",
|
||||
);
|
||||
});
|
||||
it('should append /v1 to apiBase for "ollama"', () => {
|
||||
const ollama = constructLlmApi({
|
||||
provider: "ollama",
|
||||
apiBase: "http://localhost:123",
|
||||
});
|
||||
|
||||
expect((ollama as OpenAIApi).openai.baseURL).toBe(
|
||||
"http://localhost:11434/v1/",
|
||||
);
|
||||
});
|
||||
|
||||
it('should append /v1 to apiBase for "ollama"', () => {
|
||||
const ollama = constructLlmApi({
|
||||
provider: "ollama",
|
||||
apiBase: "http://localhost:123",
|
||||
expect((ollama as OpenAIApi).openai.baseURL).toBe(
|
||||
"http://localhost:123/v1/",
|
||||
);
|
||||
});
|
||||
it("should not reappend /v1 to apibase for ollama if it is already present", () => {
|
||||
const ollama = constructLlmApi({
|
||||
provider: "ollama",
|
||||
apiBase: "http://localhost:123/v1/",
|
||||
});
|
||||
|
||||
expect((ollama as OpenAIApi).openai.baseURL).toBe(
|
||||
"http://localhost:123/v1/",
|
||||
);
|
||||
});
|
||||
|
||||
it("should not reappend /v1 to apibase for ollama if it is already present", () => {
|
||||
const ollama = constructLlmApi({
|
||||
provider: "ollama",
|
||||
apiBase: "http://localhost:123/v1/",
|
||||
expect((ollama as OpenAIApi).openai.baseURL).toBe(
|
||||
"http://localhost:123/v1/",
|
||||
);
|
||||
});
|
||||
it("should append v1 if apiBase is like myhostv1/", () => {
|
||||
const ollama = constructLlmApi({
|
||||
provider: "ollama",
|
||||
apiBase: "https://myhostv1/",
|
||||
});
|
||||
expect((ollama as OpenAIApi).openai.baseURL).toBe("https://myhostv1/v1/");
|
||||
});
|
||||
it("should preserve query params and append v1 in apibase", () => {
|
||||
const ollama = constructLlmApi({
|
||||
provider: "ollama",
|
||||
apiBase: "https://test.com:123/ollama-server?x=1",
|
||||
});
|
||||
expect((ollama as OpenAIApi).openai.baseURL).toBe(
|
||||
"https://test.com:123/ollama-server/v1/?x=1",
|
||||
);
|
||||
});
|
||||
|
||||
expect((ollama as OpenAIApi).openai.baseURL).toBe(
|
||||
"http://localhost:123/v1/",
|
||||
);
|
||||
});
|
||||
});
|
||||
|
|
|
|||
17
packages/openai-adapters/src/util/appendPathToUrl.ts
Normal file
17
packages/openai-adapters/src/util/appendPathToUrl.ts
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
export function appendPathToUrlIfNotPresent(
|
||||
urlString: string,
|
||||
pathWithoutSlash: string,
|
||||
) {
|
||||
const url = new URL(urlString);
|
||||
if (!url.pathname.endsWith("/")) {
|
||||
url.pathname += "/";
|
||||
}
|
||||
if (!url.pathname.endsWith(pathWithoutSlash + "/")) {
|
||||
url.pathname += pathWithoutSlash + "/";
|
||||
}
|
||||
if (url.search) {
|
||||
return url.toString();
|
||||
}
|
||||
// append slash at the end
|
||||
return url.toString();
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue