Skip to content

Commit

Permalink
Merge pull request #18 from AirWalk-Digital/search-feature
Browse files Browse the repository at this point in the history
Search feature - updated the langchain package to fix a bug of Python data loader for Redis.
  • Loading branch information
RemusYu committed Jan 29, 2024
2 parents 0d44bbc + 9f9506f commit b34af5f
Show file tree
Hide file tree
Showing 5 changed files with 422 additions and 297 deletions.
6 changes: 3 additions & 3 deletions .devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
"name": "Airview Next",
"dockerFile": "Dockerfile.vscode",
"remoteUser": "vscode",
"mounts": [
"source=/Users/robe/.ssh,target=/home/vscode/.ssh,type=bind,consistency=cached"
],
//"mounts": [
// "source=/Users/robe/.ssh,target=/home/vscode/.ssh,type=bind,consistency=cached"
//],
"forwardPorts": [3000, 6006],

"customizations": {
Expand Down
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ GITHUB_PRIVATE_KEY_FILE=
REDIS_HOST=172.17.0.1
REDIS_PASSWORD=
REDIS_PORT=
INDEX_NAME=

# SharePoint
SHAREPOINT_CLIENT_ID=
Expand Down
70 changes: 22 additions & 48 deletions app/api/chat/route.js
Original file line number Diff line number Diff line change
@@ -1,70 +1,44 @@
import { ChatOpenAI } from "langchain/chat_models/openai";
import { HNSWLib } from "langchain/vectorstores/hnswlib";
import { OpenAIEmbeddings } from "langchain/embeddings/openai";
import { RecursiveCharacterTextSplitter } from "langchain/text_splitter";
import { ChatOpenAI } from "@langchain/openai";
import { OpenAIEmbeddings } from "@langchain/openai";
import { BufferMemory } from "langchain/memory";
import fs from "fs";
import { RunnableBranch, RunnableSequence } from "langchain/schema/runnable";
import { PromptTemplate } from "langchain/prompts";
import { StringOutputParser } from "langchain/schema/output_parser";
import { RunnableBranch, RunnableSequence } from "@langchain/core/runnables";
import { PromptTemplate } from "@langchain/core/prompts";
import { StringOutputParser } from "@langchain/core/output_parsers";
import { LLMChain } from "langchain/chains";
import { formatDocumentsAsString } from "langchain/util/document";
import { MemoryVectorStore } from "langchain/vectorstores/memory";
import { StreamingTextResponse } from "ai";

// export const runtime = "edge";
import { RedisVectorStore } from "@langchain/community/vectorstores/redis";
import { createClient } from "redis";

export async function POST(req) {
const formatMessage = (message) => {
return `${message.role}: ${message.content}`;
};

const TEMPLATE = `You are an IT expert. All responses must be extremely verbose and technical in nature.
Answer the question based only on the following context:
{context}
Current conversation:
{chat_history}
User: {input}
AI:`;

const TEXT = `
State of the Union Address Example
My fellow Americans,
As we gather here tonight, the state of our union is strong. Our economy is growing, our citizens are prospering, and our nation is safe and secure. We have faced challenges in the past year, but our resolve has never been stronger.
In the past year, we have made significant progress in key areas. Our administration has worked tirelessly to improve the lives of every American. We have made great strides in healthcare, ensuring that more citizens have access to affordable care. Education has been at the forefront of our agenda, with increased funding for schools and support for teachers.
We have also made significant advancements in technology and innovation. Our efforts in renewable energy are paying off, leading us towards a more sustainable and environmentally friendly future. The job market continues to grow, offering opportunities in new and emerging industries.
However, there is still work to be done. We must continue to strive for equality and justice for all our citizens, regardless of their background. Infrastructure improvements are needed to keep up with the growing demands of our nation. And we must remain vigilant in protecting our country from external threats.
Looking ahead, our goals are clear. We aim to strengthen our economy further, enhance our national security, and ensure that the American dream is attainable for all. With unity and determination, there is no limit to what we can achieve.
Together, let us continue to build a brighter future for our great nation.
Thank you, God bless you, and God bless America.
`;

const body = await req.json();
const messages = body.messages ?? [];
const formattedPreviousMessages = messages.slice(0, -1).map(formatMessage);
console.log('formattedPreviousMessages: ', formattedPreviousMessages)
const currentMessageContent = messages[messages.length - 1].content;
const prompt = PromptTemplate.fromTemplate(TEMPLATE);
const OPENAI_API_KEY = process.env.OPENAI_API_KEY;
const REDIS_HOST = process.env.REDIS_HOST;

const model = new ChatOpenAI({temperature: 0.8, modelName: 'gpt-3.5-turbo', openAIApiKey: OPENAI_API_KEY});
// const text = fs.readFileSync("./state.txt", "utf8");
const textSplitter = new RecursiveCharacterTextSplitter({ chunkSize: 1000 });
const docs = await textSplitter.createDocuments(TEXT);

const redisClient = createClient({
url: process.env.REDIS_URL ?? `redis://${REDIS_HOST}:6379`,
});
await redisClient.connect();
console.log("Successfully connect to Redis");

const vectorStore = await MemoryVectorStore.fromDocuments(docs, new OpenAIEmbeddings());
const vectorStore = new RedisVectorStore(new OpenAIEmbeddings(), {
redisClient: redisClient,
indexName: process.env.INDEX_NAME,
});
//console.log("vectorStore: ", vectorStore); //for debugging

const retriever = vectorStore.asRetriever();
//console.log("Retriever: ", retriever); //for debugging

const serializeChatHistory = (chatHistory) => {
if (Array.isArray(chatHistory)) {
Expand Down Expand Up @@ -189,7 +163,7 @@ Standalone question:`);
question: currentMessageContent,
chatHistory: formattedPreviousMessages
});

return new StreamingTextResponse(stream);
// } catch (e) {
// return NextResponse.json({ error: e.message }, { status: 500 });
Expand Down
Loading

0 comments on commit b34af5f

Please sign in to comment.