Skip to content

Commit

Permalink
tests: support local models (#103)
Browse files Browse the repository at this point in the history
Add support for using a local model for testing.

Signed-off-by: Michael Dawson <midawson@redhat.com>
  • Loading branch information
mhdawson authored Oct 27, 2024
1 parent e1a3660 commit a815053
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
3 changes: 2 additions & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,13 @@ yarn install
5. **Setup environmental variables:** To run E2E Tests, you should set the following variables in your `.env` file in the repository’s root.

```bash
# At least one provider API key must be defined!
# At least one provider API key or an OLLAMA_HOST must be defined!
GENAI_API_KEY=""
OPENAI_API_KEY=""
GROQ_API_KEY=""
WATSONX_API_KEY=""
WATSONX_PROJECT_ID=""
OLLAMA_HOST=""

WATSONX_SPACE_ID="" # optional
WATSONX_DEPLOYMENT_ID="" # optional
Expand Down
28 changes: 28 additions & 0 deletions tests/utils/llmFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ import { BAMChatLLM } from "@/adapters/bam/chat.js";
import { OpenAIChatLLM } from "@/adapters/openai/chat.js";
import { WatsonXChatLLM } from "@/adapters/watsonx/chat.js";
import { GroqChatLLM } from "@/adapters/groq/chat.js";
import { OllamaChatLLM } from "@/adapters/ollama/chat.js";
import { Ollama } from "ollama";
import { Agent, Dispatcher } from "undici";

export function createChatLLM(): ChatLLM<ChatLLMOutput> {
if (process.env.GENAI_API_KEY) {
Expand All @@ -35,6 +38,31 @@ export function createChatLLM(): ChatLLM<ChatLLMOutput> {
modelId: `llama-3.1-70b-versatile`,
parameters: { temperature: 0 },
});
} else if (process.env.OLLAMA_HOST) {
// the undici definition of RequestInit does not extend the default
// fetch RequestInit so we can't use its type directly. Define
// and interface that adds the field we need to the default fetch
// interface to that we can make TypeScript accept it.
interface UndiciRequestInit extends RequestInit {
dispatcher: Dispatcher;
}
return new OllamaChatLLM({
modelId: process.env.OLLAMA_MODEL ?? "llama3.1:8b",
parameters: {
temperature: 0,
},
client: new Ollama({
host: process.env.OLLAMA_HOST,
fetch: (input, init?) => {
const someInit = init || {};
const requestInit: UndiciRequestInit = {
...someInit,
dispatcher: new Agent({ headersTimeout: 2700000 }),
};
return fetch(input, requestInit);
},
}),
});
} else {
throw new Error("No API key for any LLM provider has been provided. Cannot run test case.");
}
Expand Down

0 comments on commit a815053

Please sign in to comment.