-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
2,500 additions
and
323 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import IndexClient, { IndexVectorStore } from "@indexnetwork/sdk"; | ||
// import IndexClient, { | ||
// IndexVectorStore, | ||
// } from "../../sdk/js/dist/indexclient.es.js"; | ||
import { ChatOpenAI, OpenAIEmbeddings } from "@langchain/openai"; | ||
import { Wallet } from "ethers"; | ||
import { ConversationalRetrievalQAChain } from "langchain/chains"; | ||
|
||
async function main() { | ||
try { | ||
const wallet = new Wallet(process.env.PRIVATE_KEY); | ||
const indexClient = new IndexClient({ | ||
network: "dev", // or mainnet | ||
wallet, // or session | ||
domain: "indexd", | ||
}); | ||
|
||
// await indexClient.authenticate(); | ||
|
||
const embeddings = new OpenAIEmbeddings({ | ||
apiKey: process.env.OPENAI_API_KEY, | ||
model: "text-embedding-ada-002", | ||
}); | ||
|
||
const sourceIndexId = | ||
"kjzl6kcym7w8y7lvuklrt4mmon5h9u3wpkm9jd9rtdbghl9df2ujsyid8d0qxj4"; | ||
|
||
const vectorStore = new IndexVectorStore(embeddings, { | ||
client: indexClient, | ||
sources: [sourceIndexId], | ||
}); | ||
|
||
/* Run vector store search */ | ||
const question = "What is mesh.xyz?"; | ||
const res = await vectorStore.similaritySearch(question, 1); | ||
console.log("Retieved Documents", JSON.stringify(res, null, 3)); | ||
|
||
/* Create a QA chain */ | ||
const model = new ChatOpenAI({ | ||
apiKey: process.env.OPENAI_API_KEY, | ||
model: "gpt-3.5-turbo", | ||
}); | ||
|
||
const chain = ConversationalRetrievalQAChain.fromLLM( | ||
model, | ||
vectorStore.asRetriever(), | ||
); | ||
|
||
/* Ask it a question */ | ||
const qa_res = await chain.invoke({ question, chat_history: [] }); | ||
console.log("Chat response:", JSON.stringify(qa_res, null, 3)); | ||
} catch (err) { | ||
console.error(err); | ||
} | ||
} | ||
|
||
main(); |
Oops, something went wrong.