Skip to content

Commit

Permalink
refactor: move groq as llm package (run-llama#1209)
Browse files Browse the repository at this point in the history
  • Loading branch information
thucpn authored Sep 16, 2024
1 parent 70ccb4a commit fbd5e01
Show file tree
Hide file tree
Showing 14 changed files with 133 additions and 33 deletions.
6 changes: 6 additions & 0 deletions .changeset/nine-rules-hug.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"llamaindex": patch
"@llamaindex/groq": patch
---

refactor: move groq as llm package
3 changes: 3 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,9 @@ jobs:
- name: Pack @llamaindex/openai
run: pnpm pack --pack-destination ${{ runner.temp }}
working-directory: packages/llm/openai
- name: Pack @llamaindex/groq
run: pnpm pack --pack-destination ${{ runner.temp }}
working-directory: packages/llm/groq
- name: Pack @llamaindex/core
run: pnpm pack --pack-destination ${{ runner.temp }}
working-directory: packages/core
Expand Down
13 changes: 12 additions & 1 deletion examples/groq.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
import fs from "node:fs/promises";

import { Document, Groq, Settings, VectorStoreIndex } from "llamaindex";
import {
Document,
Groq,
HuggingFaceEmbedding,
Settings,
VectorStoreIndex,
} from "llamaindex";

// Update llm to use Groq
Settings.llm = new Groq({
apiKey: process.env.GROQ_API_KEY,
});

// Use HuggingFace for embeddings
Settings.embedModel = new HuggingFaceEmbedding({
modelType: "Xenova/all-mpnet-base-v2",
});

async function main() {
// Load essay from abramov.txt in Node
const path = "node_modules/llamaindex/examples/abramov.txt";
Expand Down
1 change: 1 addition & 0 deletions packages/llamaindex/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"@llamaindex/core": "workspace:*",
"@llamaindex/env": "workspace:*",
"@llamaindex/openai": "workspace:*",
"@llamaindex/groq": "workspace:*",
"@mistralai/mistralai": "^1.0.4",
"@mixedbread-ai/sdk": "^2.2.11",
"@pinecone-database/pinecone": "^3.0.2",
Expand Down
31 changes: 1 addition & 30 deletions packages/llamaindex/src/llm/groq.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1 @@
import { getEnv } from "@llamaindex/env";
import { OpenAI } from "@llamaindex/openai";
import GroqSDK, { type ClientOptions } from "groq-sdk";

export class Groq extends OpenAI {
constructor(
init?: Partial<OpenAI> & {
additionalSessionOptions?: ClientOptions;
},
) {
const {
apiKey = getEnv("GROQ_API_KEY"),
additionalSessionOptions = {},
model = "mixtral-8x7b-32768",
...rest
} = init ?? {};

super({
apiKey,
additionalSessionOptions,
model,
...rest,
});

this.session.openai = new GroqSDK({
apiKey,
...init?.additionalSessionOptions,
}) as any;
}
}
export * from "@llamaindex/groq";
2 changes: 1 addition & 1 deletion packages/llamaindex/src/llm/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export {
GEMINI_MODEL,
type GoogleGeminiSessionOptions,
} from "./gemini/types.js";
export { Groq } from "./groq.js";
export * from "./groq.js";
export { HuggingFaceInferenceAPI, HuggingFaceLLM } from "./huggingface.js";
export {
ALL_AVAILABLE_MISTRAL_MODELS,
Expand Down
1 change: 0 additions & 1 deletion packages/llamaindex/src/next.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ export default function withLlamaIndex(config: any) {
webpackConfig.resolve.alias = {
...webpackConfig.resolve.alias,
"@google-cloud/vertexai": false,
"groq-sdk": false,
};
// Following lines will fix issues with onnxruntime-node when using pnpm
// See: https://github.com/vercel/next.js/issues/43433
Expand Down
1 change: 1 addition & 0 deletions packages/llm/groq/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# @llamaindex/groq
40 changes: 40 additions & 0 deletions packages/llm/groq/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"name": "@llamaindex/groq",
"description": "Groq Adapter for LlamaIndex",
"version": "0.0.1",
"type": "module",
"main": "./dist/index.cjs",
"module": "./dist/index.js",
"exports": {
".": {
"require": {
"types": "./dist/index.d.cts",
"default": "./dist/index.cjs"
},
"import": {
"types": "./dist/index.d.ts",
"default": "./dist/index.js"
}
}
},
"files": [
"dist"
],
"repository": {
"type": "git",
"url": "https://github.com/run-llama/LlamaIndexTS.git",
"directory": "packages/llm/groq"
},
"scripts": {
"build": "bunchee",
"dev": "bunchee --watch"
},
"devDependencies": {
"bunchee": "5.3.2"
},
"dependencies": {
"@llamaindex/env": "workspace:*",
"@llamaindex/openai": "workspace:*",
"groq-sdk": "0.6.1"
}
}
1 change: 1 addition & 0 deletions packages/llm/groq/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { Groq } from "./llm";
30 changes: 30 additions & 0 deletions packages/llm/groq/src/llm.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { getEnv } from "@llamaindex/env";
import { OpenAI } from "@llamaindex/openai";
import GroqSDK, { type ClientOptions } from "groq-sdk";

export class Groq extends OpenAI {
constructor(
init?: Partial<OpenAI> & {
additionalSessionOptions?: ClientOptions;
},
) {
const {
apiKey = getEnv("GROQ_API_KEY"),
additionalSessionOptions = {},
model = "mixtral-8x7b-32768",
...rest
} = init ?? {};

super({
apiKey,
additionalSessionOptions,
model,
...rest,
});

this.session.openai = new GroqSDK({
apiKey,
...init?.additionalSessionOptions,
}) as any;
}
}
15 changes: 15 additions & 0 deletions packages/llm/groq/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"extends": "../../../tsconfig.json",
"compilerOptions": {
"target": "ESNext",
"module": "ESNext",
"moduleResolution": "bundler",
"outDir": "./lib"
},
"include": ["./src"],
"references": [
{
"path": "../../env/tsconfig.json"
}
]
}
19 changes: 19 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
{
"path": "./packages/llm/openai/tsconfig.json"
},
{
"path": "./packages/llm/groq/tsconfig.json"
},
{
"path": "./packages/cloud/tsconfig.json"
},
Expand Down

0 comments on commit fbd5e01

Please sign in to comment.