Created
October 2, 2025 15:25
-
-
Save anuran-roy/9d23613532d0bc8ac635ab0f891d026f to your computer and use it in GitHub Desktop.
Alchemyst AI SDK in Langchain JS (Beta branch)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { StringOutputParser } from "@langchain/core/output_parsers"; | |
| import { RunnableSequence } from "@langchain/core/runnables"; | |
| import dotenv from "dotenv"; | |
| import { AlchemystRetriever } from "langchainjs/libs/langchain-community/src/retrievers/alchemystai"; | |
| // filepath: /Users/anuran/Alchemyst/integrations/playground/example.ts | |
| dotenv.config(); | |
| // Instantiate the retriever with your API key and optional config | |
| const retriever = new AlchemystRetriever({ | |
| apiKey: process.env.ALCHEMYST_AI_API_KEY!, | |
| similarityThreshold: 0.8, | |
| minimumSimilarityThreshold: 0.5, | |
| scope: "internal" | |
| }); | |
| // Example: Use the retriever in a LangChain pipeline | |
| export async function main() { | |
| // Create a simple pipeline that retrieves documents and outputs their content | |
| const pipeline = RunnableSequence.from([ | |
| async (input: string) => { | |
| const docs = await retriever.getRelevantDocuments(input); | |
| return docs.map(doc => doc.pageContent).join("\n---\n"); | |
| }, | |
| new StringOutputParser() | |
| ]); | |
| const query = "Tell me about Quantum Entanglement"; // Put your query here | |
| const result = await pipeline.invoke(query); | |
| console.log("Retrieved Documents:\n", result); | |
| } | |
| // main().catch(console.error); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { ChatPromptTemplate, MessagesPlaceholder } from "@langchain/core/prompts"; | |
| import { RunnablePassthrough, RunnableSequence } from "@langchain/core/runnables"; | |
| import { ChatOpenAI } from "@langchain/openai"; | |
| import { randomUUID } from "crypto"; | |
| import 'dotenv/config'; | |
| import { AlchemystMemory } from "langchainjs/libs/langchain-community/src/memory/alchemystai"; | |
| export async function main() { | |
| console.log("Boot: starting test with env:", { | |
| OPENAI_API_KEY: process.env.OPENAI_API_KEY ? "set" : "missing", | |
| ALCHEMYST_AI_API_KEY: process.env.ALCHEMYST_AI_API_KEY ? "set" : "missing", | |
| }); | |
| const sessionId = randomUUID(); | |
| console.log("Session:", sessionId); | |
| const memory = new AlchemystMemory({ | |
| apiKey: process.env.ALCHEMYST_AI_API_KEY || "YOUR_ALCHEMYST_API_KEY", | |
| sessionId, | |
| }); | |
| const model = new ChatOpenAI({ | |
| model: "gpt-5-nano", | |
| }); | |
| // Create a prompt template with message history | |
| const prompt = ChatPromptTemplate.fromMessages([ | |
| ["system", "You are a helpful AI assistant. Have a conversation with the human, using the chat history for context."], | |
| new MessagesPlaceholder("history"), | |
| ["human", "{input}"], | |
| ]); | |
| // Create the chain using LCEL (LangChain Expression Language) | |
| const chain = RunnableSequence.from([ | |
| RunnablePassthrough.assign({ | |
| history: async () => { | |
| const memoryVars = await memory.loadMemoryVariables({}); | |
| return memoryVars.history || []; | |
| }, | |
| }), | |
| prompt, | |
| model, | |
| ]); | |
| console.log("Invoke #1 ->"); | |
| const first = await chain.invoke({ input: "Hi, my name is Anuran. Anuran is from Bangalore." }); | |
| // Save to memory | |
| await memory.saveContext( | |
| { input: "Hi, my name is Anuran. Anuran is from Bangalore." }, | |
| { output: first.content } | |
| ); | |
| console.log("First reply:", first.content); | |
| console.log("Invoke #2 ->"); | |
| const second = await chain.invoke({ input: "Who is Anuran? Where is Anuran from?" }); | |
| // Save to memory | |
| await memory.saveContext( | |
| { input: "Who is Anuran? Where is Anuran from?" }, | |
| { output: second.content } | |
| ); | |
| console.log("Second reply:", second.content); | |
| } | |
| // main().catch((err) => { | |
| // console.error(err); | |
| // process.exit(1); | |
| // }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { main as exampleOneMain } from './example1'; | |
| import { main as exampleTwoMain } from './example2'; | |
| exampleOneMain().catch(console.error); | |
| exampleTwoMain().catch(console.error); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "name": "playground", | |
| "module": "index.ts", | |
| "type": "module", | |
| "private": true, | |
| "devDependencies": { | |
| "@types/bun": "latest" | |
| }, | |
| "peerDependencies": { | |
| "typescript": "^5" | |
| }, | |
| "dependencies": { | |
| "@alchemystai/sdk": "^0.5.0", | |
| "@langchain/core": "^0.3.78", | |
| "@langchain/openai": "^0.6.14", | |
| "dotenv": "^17.2.3", | |
| "langchain": "^0.3.35", | |
| "langchainjs": "https://github.com/Alchemyst-ai/langchainjs.git#add-alchemyst-sdk" | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Show hidden characters
| { | |
| "compilerOptions": { | |
| // Environment setup & latest features | |
| "lib": ["ESNext"], | |
| "target": "ESNext", | |
| "module": "Preserve", | |
| "moduleDetection": "force", | |
| "jsx": "react-jsx", | |
| "allowJs": true, | |
| // Bundler mode | |
| "moduleResolution": "bundler", | |
| "allowImportingTsExtensions": true, | |
| "verbatimModuleSyntax": true, | |
| "noEmit": true, | |
| // Best practices | |
| "strict": true, | |
| "skipLibCheck": true, | |
| "noFallthroughCasesInSwitch": true, | |
| "noUncheckedIndexedAccess": true, | |
| "noImplicitOverride": true, | |
| // Some stricter flags (disabled by default) | |
| "noUnusedLocals": false, | |
| "noUnusedParameters": false, | |
| "noPropertyAccessFromIndexSignature": false | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment