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
B_INST, E_INST = "[INST]", "[/INST]"
@ -11,7 +12,7 @@ QA_CONDENSE_TEMPLATE_STR = (
" with the new Standalone Question. \n"
"Chat History: \n"
"{chat_history} \n"
"Follow Up Question: {input} \n"
"Follow Up Question: {question} \n"
"Standalone Question:"
)
@ -28,7 +29,7 @@ QA_PROMPT_TEMPLATE_STR = (
"don't try to make up an answer. \n\n"
"{context}\n"
"{chat_history}\n"
"{input}\n\n"
"{question}\n\n"
"AI: Here is the most relevant sentence in the context: \n"
)
@ -75,4 +76,4 @@ _SUMMARIZER_INST_TEMPLATE = (
SUMMARY_PROMPT_TEMPLATE = PromptTemplate.from_template(
template=(_SUMMARIZER_SYS_TEMPLATE + "\n" + _SUMMARIZER_INST_TEMPLATE).strip()
)
)

@ -11,7 +11,8 @@ from langchain.callbacks.manager import (
from langchain.retrievers import ParentDocumentRetriever
from langchain.schema.document import Document
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

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

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

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

Loading…
Cancel
Save