feat: add xai support (#1947)

This commit is contained in:
Marcus Schiesser
2025-05-13 16:48:53 +07:00
committed by GitHub
parent c2fd4f9fc1
commit 7a7ca604c5
10 changed files with 157 additions and 15 deletions
+5
View File
@@ -0,0 +1,5 @@
---
"@llamaindex/xai": patch
---
Initial release of the XAI provider
+5
View File
@@ -0,0 +1,5 @@
---
"@llamaindex/fireworks": patch
---
Make env package a peer deps
+18
View File
@@ -0,0 +1,18 @@
import { xai } from "@llamaindex/xai";
(async () => {
// note: set XAI_API_KEY in your environment
// you can also pass the API key in the constructor, for example:
// const llm = xai({ apiKey: "YOUR_API_KEY" });
const llm = xai({ model: "grok-3-latest" });
// complete api
const response1 = await llm.complete({ prompt: "How are you?" });
console.log(response1.text);
// chat api
const response2 = await llm.chat({
messages: [{ content: "Tell me a joke.", role: "user" }],
});
console.log(response2.message.content);
})();
+12 -11
View File
@@ -12,6 +12,7 @@
"@azure/identity": "^4.4.1",
"@azure/search-documents": "^12.1.0",
"@llamaindex/anthropic": "^0.3.5",
"@llamaindex/assemblyai": "^0.1.3",
"@llamaindex/astra": "^0.0.18",
"@llamaindex/azure": "^0.1.14",
"@llamaindex/chroma": "^0.0.18",
@@ -20,43 +21,43 @@
"@llamaindex/cohere": "^0.0.18",
"@llamaindex/core": "^0.6.4",
"@llamaindex/deepinfra": "^0.0.54",
"@llamaindex/deepseek": "^0.0.14",
"@llamaindex/discord": "^0.1.3",
"@llamaindex/elastic-search": "^0.1.4",
"@llamaindex/env": "^0.1.30",
"@llamaindex/firestore": "^1.0.11",
"@llamaindex/fireworks": "^0.0.14",
"@llamaindex/google": "^0.3.0",
"@llamaindex/groq": "^0.0.69",
"@llamaindex/huggingface": "^0.1.8",
"@llamaindex/jinaai": "^0.0.14",
"@llamaindex/milvus": "^0.1.13",
"@llamaindex/mistral": "^0.1.4",
"@llamaindex/mixedbread": "^0.0.18",
"@llamaindex/mongodb": "^0.0.19",
"@llamaindex/elastic-search": "^0.1.4",
"@llamaindex/node-parser": "^2.0.4",
"@llamaindex/notion": "^0.1.3",
"@llamaindex/ollama": "^0.1.4",
"@llamaindex/openai": "^0.3.6",
"@llamaindex/perplexity": "^0.0.11",
"@llamaindex/pinecone": "^0.1.4",
"@llamaindex/portkey-ai": "^0.0.46",
"@llamaindex/postgres": "^0.0.47",
"@llamaindex/qdrant": "^0.1.13",
"@llamaindex/readers": "^3.1.2",
"@llamaindex/replicate": "^0.0.46",
"@llamaindex/supabase": "^0.1.3",
"@llamaindex/together": "^0.0.14",
"@llamaindex/tools": "^0.0.9",
"@llamaindex/upstash": "^0.0.18",
"@llamaindex/vercel": "^0.1.4",
"@llamaindex/vllm": "^0.0.40",
"@llamaindex/voyage-ai": "^1.0.10",
"@llamaindex/weaviate": "^0.0.18",
"@llamaindex/workflow": "^1.1.1",
"@llamaindex/deepseek": "^0.0.14",
"@llamaindex/fireworks": "^0.0.14",
"@llamaindex/together": "^0.0.14",
"@llamaindex/jinaai": "^0.0.14",
"@llamaindex/perplexity": "^0.0.11",
"@llamaindex/supabase": "^0.1.3",
"@llamaindex/tools": "^0.0.9",
"@llamaindex/xai": "workspace:^0.0.1",
"@notionhq/client": "^2.2.15",
"@pinecone-database/pinecone": "^4.0.0",
"@llamaindex/assemblyai": "^0.1.3",
"@llamaindex/discord": "^0.1.3",
"@llamaindex/notion": "^0.1.3",
"@vercel/postgres": "^0.10.0",
"ai": "^4.0.0",
"ajv": "^8.17.1",
+6 -1
View File
@@ -30,7 +30,12 @@
"dev": "bunchee --watch"
},
"dependencies": {
"@llamaindex/env": "workspace:*",
"@llamaindex/openai": "workspace:*"
},
"devDependencies": {
"@llamaindex/env": "workspace:*"
},
"peerDependencies": {
"@llamaindex/env": "workspace:*"
}
}
+40
View File
@@ -0,0 +1,40 @@
{
"name": "@llamaindex/xai",
"description": "XAI 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": "git+https://github.com/run-llama/LlamaIndexTS.git",
"directory": "packages/providers/xai"
},
"scripts": {
"build": "bunchee",
"dev": "bunchee --watch"
},
"dependencies": {
"@llamaindex/env": "workspace:*",
"@llamaindex/openai": "workspace:*"
},
"devDependencies": {},
"peerDependencies": {
"@llamaindex/env": "workspace:*"
}
}
+1
View File
@@ -0,0 +1 @@
export * from "./llm";
+35
View File
@@ -0,0 +1,35 @@
import { getEnv } from "@llamaindex/env";
import { OpenAI } from "@llamaindex/openai";
export class XAILLM extends OpenAI {
constructor(init?: Omit<Partial<OpenAI>, "session">) {
const {
apiKey = getEnv("XAI_API_KEY"),
additionalSessionOptions = {},
model = "grok-3-latest",
...rest
} = init ?? {};
if (!apiKey) {
throw new Error("Set API Key in XAI_API_KEY environment variable");
}
additionalSessionOptions.baseURL =
additionalSessionOptions.baseURL ?? "https://api.x.ai/v1";
super({
apiKey,
additionalSessionOptions,
model,
...rest,
});
}
}
/**
* Convenience function to create a new XAILLM instance.
* @param init - Optional initialization parameters for the XAILLM instance.
* @returns A new XAILLM instance.
*/
export const xai = (init?: ConstructorParameters<typeof XAILLM>[0]) =>
new XAILLM(init);
+19
View File
@@ -0,0 +1,19 @@
{
"extends": "../../../tsconfig.json",
"compilerOptions": {
"target": "ESNext",
"module": "ESNext",
"moduleResolution": "bundler",
"outDir": "./lib",
"tsBuildInfoFile": "./lib/.tsbuildinfo"
},
"include": ["./src"],
"references": [
{
"path": "../openai/tsconfig.json"
},
{
"path": "../../env/tsconfig.json"
}
]
}
+16 -3
View File
@@ -740,6 +740,9 @@ importers:
'@llamaindex/workflow':
specifier: ^1.1.1
version: link:../packages/workflow
'@llamaindex/xai':
specifier: workspace:^0.0.1
version: link:../packages/providers/xai
'@notionhq/client':
specifier: ^2.2.15
version: 2.3.0
@@ -1193,12 +1196,13 @@ importers:
packages/providers/fireworks:
dependencies:
'@llamaindex/env':
specifier: workspace:*
version: link:../../env
'@llamaindex/openai':
specifier: workspace:*
version: link:../openai
devDependencies:
'@llamaindex/env':
specifier: workspace:*
version: link:../../env
packages/providers/google:
dependencies:
@@ -1670,6 +1674,15 @@ importers:
specifier: workspace:*
version: link:../../env
packages/providers/xai:
dependencies:
'@llamaindex/env':
specifier: workspace:*
version: link:../../env
'@llamaindex/openai':
specifier: workspace:*
version: link:../openai
packages/readers:
dependencies:
'@discoveryjs/json-ext':