fix deprecated langchain stuff

pull/570/head
Richard Anthony Hein 8 months ago
parent 14ef3957ac
commit bd5321631b

@ -1,3 +1,4 @@
""" Prompt templates for Swarms Chatbot """
from langchain.prompts.prompt import PromptTemplate from langchain.prompts.prompt import PromptTemplate
B_INST, E_INST = "[INST]", "[/INST]" B_INST, E_INST = "[INST]", "[/INST]"
@ -11,7 +12,7 @@ QA_CONDENSE_TEMPLATE_STR = (
" with the new Standalone Question. \n" " with the new Standalone Question. \n"
"Chat History: \n" "Chat History: \n"
"{chat_history} \n" "{chat_history} \n"
"Follow Up Question: {input} \n" "Follow Up Question: {question} \n"
"Standalone Question:" "Standalone Question:"
) )
@ -28,7 +29,7 @@ QA_PROMPT_TEMPLATE_STR = (
"don't try to make up an answer. \n\n" "don't try to make up an answer. \n\n"
"{context}\n" "{context}\n"
"{chat_history}\n" "{chat_history}\n"
"{input}\n\n" "{question}\n\n"
"AI: Here is the most relevant sentence in the context: \n" "AI: Here is the most relevant sentence in the context: \n"
) )

@ -11,7 +11,8 @@ from langchain.callbacks.manager import (
from langchain.retrievers import ParentDocumentRetriever from langchain.retrievers import ParentDocumentRetriever
from langchain.schema.document import Document from langchain.schema.document import Document
from langchain.schema.storage import BaseStore from langchain.schema.storage import BaseStore
from langchain.storage import LocalFileStore, RedisStore from langchain.storage import LocalFileStore
from langchain_community.storage import RedisStore
from langchain.vectorstores.base import VectorStore from langchain.vectorstores.base import VectorStore

@ -113,21 +113,20 @@ langchain.verbose = True
@asynccontextmanager @asynccontextmanager
async def lifespan(app: FastAPI): async def lifespan(app: FastAPI):
"""Initializes the vector store in a background task.""" """Initializes the vector store in a background task."""
print(f"Initializing vector store retrievers for {app.title}.")
asyncio.create_task(vector_store.init_retrievers()) asyncio.create_task(vector_store.init_retrievers())
yield yield
chatbot = FastAPI(title="Chatbot", lifespan=lifespan) app = FastAPI(title="Chatbot", lifespan=lifespan)
router = APIRouter() router = APIRouter()
current_dir = os.path.dirname(__file__) current_dir = os.path.dirname(__file__)
print("current_dir: " + current_dir) print("current_dir: " + current_dir)
static_dir = os.path.join(current_dir, "static") static_dir = os.path.join(current_dir, "static")
print("static_dir: " + static_dir) print("static_dir: " + static_dir)
chatbot.mount(static_dir, StaticFiles(directory=static_dir), name="static") app.mount(static_dir, StaticFiles(directory=static_dir), name="static")
chatbot.add_middleware( app.add_middleware(
CORSMiddleware, CORSMiddleware,
allow_origins=["*"], allow_origins=["*"],
allow_credentials=True, allow_credentials=True,
@ -170,7 +169,7 @@ async def create_chain(
# if llm is VLLMAsync: # if llm is VLLMAsync:
# llm.max_tokens = max_tokens_to_gen # llm.max_tokens = max_tokens_to_gen
retriever = await vector_store.get_retriever() retriever = await vector_store.get_retriever("swarms")
chat_memory = ChatMessageHistory() chat_memory = ChatMessageHistory()
for message in messages: for message in messages:
@ -222,11 +221,7 @@ async def create_chain(
verbose=True, verbose=True,
) )
@app.post(
router = APIRouter()
@router.post(
"/chat", "/chat",
summary="Chatbot", summary="Chatbot",
description="Chatbot AI Service", description="Chatbot AI Service",
@ -252,25 +247,22 @@ async def chat(request: ChatRequest):
# ], # ],
} }
return LangchainStreamingResponse( return LangchainStreamingResponse(
chain, chain=chain,
config=json_config, config=json_config,
run_mode="async"
) )
@app.get("/")
chatbot.include_router(router, tags=["chat"])
@chatbot.get("/")
def root(): def root():
"""Swarms Chatbot API Root""" """Swarms Chatbot API Root"""
return {"message": "Swarms Chatbot API"} return {"message": "Swarms Chatbot API"}
@chatbot.get("/favicon.ico") @app.get("/favicon.ico")
def favicon(): def favicon():
""" Returns a favicon """ """ Returns a favicon """
file_name = "favicon.ico" file_name = "favicon.ico"
file_path = os.path.join(chatbot.root_path, "static", file_name) file_path = os.path.join(app.root_path, "static", file_name)
return FileResponse( return FileResponse(
path=file_path, path=file_path,
headers={ headers={
@ -282,12 +274,12 @@ def favicon():
logging.basicConfig(level=logging.ERROR) logging.basicConfig(level=logging.ERROR)
@chatbot.exception_handler(HTTPException) # @app.exception_handler(HTTPException)
async def http_exception_handler(r: Request, exc: HTTPException): # async def http_exception_handler(r: Request, exc: HTTPException):
"""Log and return exception details in response.""" # """Log and return exception details in response."""
logging.error( # logging.error(
"HTTPException: %s executing request: %s", exc.detail, r.base_url # "HTTPException: %s executing request: %s", exc.detail, r.base_url
) # )
return JSONResponse( # return JSONResponse(
status_code=exc.status_code, content={"detail": exc.detail} # status_code=exc.status_code, content={"detail": exc.detail}
) # )

@ -1,8 +1,5 @@
""" Chatbot Server API Models """ """ Chatbot Server API Models """
try: from strenum import StrEnum
from enum import StrEnum
except ImportError:
from strenum import StrEnum
from pydantic import BaseModel from pydantic import BaseModel
from swarms.prompts import QA_PROMPT_TEMPLATE_STR as DefaultSystemPrompt from swarms.prompts import QA_PROMPT_TEMPLATE_STR as DefaultSystemPrompt

@ -1,21 +1,22 @@
""" Vector storage with RAG (Retrieval Augmented Generation) support for Markdown.""" """ Vector storage with RAG (Retrieval Augmented Generation) support for Markdown."""
import asyncio import asyncio
import glob
import json import json
import os import os
import glob
from datetime import datetime from datetime import datetime
from typing import Dict from typing import Dict
from chromadb.config import Settings from chromadb.config import Settings
from langchain.document_loaders.markdown import UnstructuredMarkdownLoader from langchain_community.document_loaders import UnstructuredMarkdownLoader
from langchain.embeddings import HuggingFaceBgeEmbeddings from langchain_community.embeddings import HuggingFaceBgeEmbeddings
from langchain.schema import BaseRetriever
from langchain.storage import LocalFileStore from langchain.storage import LocalFileStore
from langchain.text_splitter import RecursiveCharacterTextSplitter from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain.vectorstores.chroma import Chroma from langchain_chroma import Chroma
from langchain.schema import BaseRetriever
from swarms.server.async_parent_document_retriever import ( from swarms.server.async_parent_document_retriever import \
AsyncParentDocumentRetriever, AsyncParentDocumentRetriever
)
STORE_TYPE = "local" # "redis" or "local" STORE_TYPE = "local" # "redis" or "local"
@ -39,8 +40,8 @@ class VectorStorage:
chunk_size=2000, chunk_overlap=200 chunk_size=2000, chunk_overlap=200
) )
if STORE_TYPE == "redis": if STORE_TYPE == "redis":
from langchain.storage import RedisStore from langchain_community.storage import RedisStore
from langchain.utilities.redis import get_client from langchain_community.storage.redis import get_client
username = r"username" username = r"username"
password = r"password" password = r"password"
@ -60,9 +61,7 @@ class VectorStorage:
self.client = self.vectorstore._client self.client = self.vectorstore._client
self.retrievers: Dict[str, BaseRetriever] = {} self.retrievers: Dict[str, BaseRetriever] = {}
# default retriever for when no collection title is specified # default retriever for when no collection title is specified
self.retrievers[ self.retrievers["swarms"] = self.vectorstore.as_retriever()
str(self.vectorstore._LANGCHAIN_DEFAULT_COLLECTION_NAME)
] = self.vectorstore.as_retriever()
async def init_retrievers(self, directories: list[str] | None = None): async def init_retrievers(self, directories: list[str] | None = None):
"""Initializes the vector storage retrievers.""" """Initializes the vector storage retrievers."""
@ -235,7 +234,7 @@ class VectorStorage:
def get_vector_store(self, collection_name: str | None = None) -> Chroma: def get_vector_store(self, collection_name: str | None = None) -> Chroma:
""" get a specific vector store for a collection """ """ get a specific vector store for a collection """
if collection_name is None or "" or "None": if collection_name is None or "" or "None":
collection_name = "langchain" collection_name = "swarms"
print("collection_name: " + collection_name) print("collection_name: " + collection_name)
vectorstore = Chroma( vectorstore = Chroma(
client_settings=self.settings, client_settings=self.settings,

Loading…
Cancel
Save