-
Notifications
You must be signed in to change notification settings - Fork 478
Implemented vector index and MCP tool for semantic search #311
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| {{- if eq .Values.chatbot.storage.type "manual" }} | ||
| apiVersion: v1 | ||
| kind: PersistentVolume | ||
| metadata: | ||
| name: {{ .Values.chatbot.storage.pv.name }} | ||
| labels: | ||
| release: {{ .Release.Name }} | ||
| {{- toYaml .Values.chatbot.storage.pv.labels | nindent 4 }} | ||
| spec: | ||
| storageClassName: {{ .Values.chatbot.storage.type }} | ||
| capacity: | ||
| storage: {{ .Values.chatbot.storage.pv.resources.storage }} | ||
| accessModes: | ||
| - ReadWriteOnce | ||
| hostPath: | ||
| path: {{ .Values.chatbot.storage.pv.hostPath }} | ||
| --- | ||
| {{- end }} | ||
| apiVersion: v1 | ||
| kind: PersistentVolumeClaim | ||
| metadata: | ||
| name: {{ .Values.chatbot.storage.pvc.name }} | ||
| labels: | ||
| release: {{ .Release.Name }} | ||
| {{- toYaml .Values.chatbot.storage.pvc.labels | nindent 4 }} | ||
| spec: | ||
| {{- if ne .Values.chatbot.storage.type "default" }} | ||
| storageClassName: {{ .Values.chatbot.storage.type }} | ||
| {{- end }} | ||
| accessModes: | ||
| - ReadWriteOnce | ||
| resources: | ||
| {{- toYaml .Values.chatbot.storage.pvc.resources | nindent 4 }} |
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
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
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
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| from langchain_community.embeddings import OpenAIEmbeddings | ||
| from langchain_community.vectorstores import Chroma | ||
| from langchain_core.documents import Document | ||
| from .config import Config | ||
|
|
||
| async def update_vector_index(api_key, session_id, new_messages): | ||
| docs = [] | ||
| for role, content in new_messages.items(): | ||
| if content: | ||
| doc = Document( | ||
| page_content=content, | ||
| metadata={"session_id": session_id, "role": role} | ||
| ) | ||
| docs.append(doc) | ||
|
|
||
| if docs: | ||
| embeddings = OpenAIEmbeddings(api_key=api_key, model="text-embedding-3-large") | ||
| vectorstore = Chroma( | ||
| embedding_function=embeddings, | ||
| persist_directory=Config.CHROMA_PERSIST_DIRECTORY | ||
| ) | ||
| vectorstore.add_documents(docs) | ||
| vectorstore.persist() |
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| import os | ||
| from langchain_community.embeddings import OpenAIEmbeddings | ||
| from langchain_community.vectorstores import Chroma | ||
| from langchain.prompts import PromptTemplate | ||
| from chatbot.extensions import db | ||
| from chatbot.config import Config | ||
| from langchain.chains import RetrievalQA | ||
| from langchain_openai import ChatOpenAI | ||
|
|
||
| retrieval_index_path = "/app/resources/chat_index" | ||
|
|
||
| async def get_any_api_key(): | ||
| if os.environ.get("CHATBOT_OPENAI_API_KEY"): | ||
| return os.environ.get("CHATBOT_OPENAI_API_KEY") | ||
| doc = await db.sessions.find_one( | ||
| {"openai_api_key": {"$exists": True, "$ne": None}}, | ||
| {"openai_api_key": 1} | ||
| ) | ||
| if doc and "openai_api_key" in doc: | ||
| return doc["openai_api_key"] | ||
| return None | ||
|
|
||
| async def get_chat_history_retriever(api_key: str): | ||
| prompt_template = PromptTemplate.from_template( | ||
| """You are an assistant that summarizes chat history across sessions. | ||
|
|
||
| Given the following chat excerpts: | ||
| {context} | ||
| Answer the user's question: {question} | ||
|
|
||
| If the user asks for a summary, provide a coherent, high-level summary of the conversations in natural language. | ||
| If the user asks a specific question, extract and answer it from the chats. | ||
| Be detailed, accurate, and neutral.""" | ||
| ) | ||
| embeddings = OpenAIEmbeddings(api_key=api_key, model="text-embedding-3-large") | ||
| vectorstore = Chroma( | ||
| embedding_function=embeddings, | ||
| persist_directory=Config.CHROMA_PERSIST_DIRECTORY | ||
| ) | ||
| retriever = vectorstore.as_retriever(search_type="similarity", search_kwargs={"k": 5}) | ||
| qa_chain = RetrievalQA.from_chain_type( | ||
| llm=ChatOpenAI(api_key=api_key, model="gpt-4o"), | ||
| retriever=retriever, | ||
| chain_type="stuff", | ||
| chain_type_kwargs={"prompt": prompt_template, "document_variable_name": "context"}, | ||
| return_source_documents=False, | ||
| ) | ||
| return qa_chain | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.