Skip to content
Snippets Groups Projects
Unverified Commit 631f0001 authored by Oguz Vuruskaner's avatar Oguz Vuruskaner Committed by GitHub
Browse files

feat: DeepInfra LLM implementation (#894)


Co-authored-by: default avatarAlex Yang <himself65@outlook.com>
parent 060b700e
No related branches found
No related tags found
No related merge requests found
---
"docs": patch
"llamaindex": patch
---
feat: DeepInfra LLM implementation
# DeepInfra
Check out available LLMs [here](https://deepinfra.com/models/text-generation).
```ts
import { DeepInfra, Settings } from "llamaindex";
// Get the API key from `DEEPINFRA_API_TOKEN` environment variable
import { config } from "dotenv";
config();
Settings.llm = new DeepInfra();
// Set the API key
apiKey = "YOUR_API_KEY";
Settings.llm = new DeepInfra({ apiKey });
```
You can setup the apiKey on the environment variables, like:
```bash
export DEEPINFRA_API_TOKEN="<YOUR_API_KEY>"
```
## Load and index documents
For this example, we will use a single document. In a real-world scenario, you would have multiple documents to index.
```ts
const document = new Document({ text: essay, id_: "essay" });
const index = await VectorStoreIndex.fromDocuments([document]);
```
## Query
```ts
const queryEngine = index.asQueryEngine();
const query = "What is the meaning of life?";
const results = await queryEngine.query({
query,
});
```
## Full Example
```ts
import { DeepInfra, Document, VectorStoreIndex, Settings } from "llamaindex";
// Use custom LLM
const model = "meta-llama/Meta-Llama-3-8B-Instruct";
Settings.llm = new DeepInfra({ model, temperature: 0 });
async function main() {
const document = new Document({ text: essay, id_: "essay" });
// Load and index documents
const index = await VectorStoreIndex.fromDocuments([document]);
// get retriever
const retriever = index.asRetriever();
// Create a query engine
const queryEngine = index.asQueryEngine({
retriever,
});
const query = "What is the meaning of life?";
// Query
const response = await queryEngine.query({
query,
});
// Log the response
console.log(response.response);
}
```
## Feedback
If you have any feedback, please reach out to us at [feedback@deepinfra.com](mailto:feedback@deepinfra.com)
import { DeepInfra } from "llamaindex";
(async () => {
if (!process.env.DEEPINFRA_API_TOKEN) {
throw new Error("Please set the DEEPINFRA_API_TOKEN environment variable.");
}
const deepinfra = new DeepInfra({});
const result = await deepinfra.chat({
messages: [
{ content: "You want to talk in rhymes.", role: "system" },
{
content:
"How much wood would a woodchuck chuck if a woodchuck could chuck wood?",
role: "user",
},
],
});
console.log(result);
})();
import { getEnv } from "@llamaindex/env";
import { OpenAI } from "./openai.js";
const ENV_VARIABLE_NAME = "DEEPINFRA_API_TOKEN";
const DEFAULT_MODEL = "mistralai/Mixtral-8x22B-Instruct-v0.1";
const BASE_URL = "https://api.deepinfra.com/v1/openai";
export class DeepInfra extends OpenAI {
constructor(init?: Partial<OpenAI>) {
const {
apiKey = getEnv(ENV_VARIABLE_NAME),
additionalSessionOptions = {},
model = DEFAULT_MODEL,
...rest
} = init ?? {};
if (!apiKey) {
throw new Error(
`Set DeepInfra API key in ${ENV_VARIABLE_NAME} env variable`,
);
}
additionalSessionOptions.baseURL =
additionalSessionOptions.baseURL ?? BASE_URL;
super({
apiKey,
additionalSessionOptions,
model,
...rest,
});
}
}
......@@ -25,6 +25,7 @@ export * from "./openai.js";
export { Portkey } from "./portkey.js";
export * from "./replicate_ai.js";
// Note: The type aliases for replicate are to simplify usage for Llama 2 (we're using replicate for Llama 2 support)
export { DeepInfra } from "./deepinfra.js";
export { Ollama, type OllamaParams } from "./ollama.js";
export {
ALL_AVAILABLE_REPLICATE_MODELS,
......@@ -34,5 +35,6 @@ export {
ReplicateLLM,
ReplicateSession,
} from "./replicate_ai.js";
export { TogetherLLM } from "./together.js";
export * from "./types.js";
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment