Skip to content

Commit

Permalink
feat(Add support for ConversationalRetrievalChain): (#37)
Browse files Browse the repository at this point in the history
  • Loading branch information
msoedov authored Sep 19, 2023
1 parent 3bd3e86 commit 17f804e
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 6 deletions.
1 change: 1 addition & 0 deletions examples/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@
"examples.ex10_cc:chain",
"examples.ex12:run",
"examples.ex13:chain",
"examples.ex14:chain",
)
26 changes: 26 additions & 0 deletions examples/ex14.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from langchain.chains import ConversationalRetrievalChain
from langchain.chat_models import ChatOpenAI
from langchain.document_loaders import TextLoader
from langchain.embeddings.fake import FakeEmbeddings
from langchain.embeddings.openai import OpenAIEmbeddings
from langchain.llms import OpenAI
from langchain.text_splitter import CharacterTextSplitter
from langchain.vectorstores import Chroma

loader = TextLoader("Readme.md")
documents = loader.load()
text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)
texts = text_splitter.split_documents(documents)

embeddings = FakeEmbeddings(size=1504)
docsearch = Chroma.from_documents(texts, embeddings)

chain = ConversationalRetrievalChain.from_llm(
llm=ChatOpenAI(model="gpt-3.5-turbo"),
retriever=docsearch.as_retriever(search_kwargs={"k": 1}),
)


if __name__ == "__main__":
# Run the chain only specifying the input variable.
print(chain.run(question="colorful socks", chat_history=[]))
10 changes: 10 additions & 0 deletions langcorn/server/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,17 @@ def derive_fields(language_app) -> (list[str], list[str]):
return language_app.input_variables, language_app.output_variables
elif hasattr(language_app, "prompt"):
return language_app.prompt.input_variables, [language_app.output_key]
elif hasattr(language_app, "input_keys"):
return language_app.input_keys, [language_app.output_key]
return [language_app.input_key], ["output"]


def derive_class(name, fields, add_memory=False):
annotations = {f: str for f in fields}
if add_memory:
annotations["memory"] = list[dict]
if "chat_history" in annotations:
annotations["chat_history"] = list[list[str]]
return type(f"Lang{name}", (BaseModel,), {"__annotations__": annotations})


Expand Down Expand Up @@ -133,6 +137,11 @@ def configure_llm(chain, http_headers: dict[str, str]):
return False


def add_chat_history(run_params):
if "chat_history" in run_params:
run_params["chat_history"] = [tuple(t) for t in run_params["chat_history"]]


def make_handler(request_cls, chain):
async def handler(request: request_cls, http_request: Request):
llm_api_key = http_request.headers.get("x-llm-api-key")
Expand All @@ -144,6 +153,7 @@ async def handler(request: request_cls, http_request: Request):
memory = run_params.pop("memory", [])
if chain.memory and memory and memory[0]:
chain.memory.chat_memory.messages = messages_from_dict(memory)
add_chat_history(run_params)
with get_openai_callback() as cb:
if not retrieval_chain:
output = chain.run(run_params)
Expand Down
10 changes: 4 additions & 6 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
[tool.poetry]
name = "langcorn"
version = "0.0.16"
version = "0.0.17"
description = "A Python package creating rest api interface for LangChain"
authors = ["Alexander Miasoiedov <msoedov@gmail.com>"]
maintainers = [
"Alexander Miasoiedov <msoedov@gmail.com>",
]
maintainers = ["Alexander Miasoiedov <msoedov@gmail.com>"]
repository = "https://github.com/msoedov/langcorn"
license = "MIT"
readme = "Readme.md"
Expand All @@ -24,7 +22,7 @@ langchain = "^0.0.283"
openai = "^0.28.0"
fire = "^0.5.0"
loguru = "^0.7.0"
bs4 = "0.0.1" # required for ex4.py
bs4 = "0.0.1" # required for ex4.py
langchain-experimental = "^0.0.14"
certifi = "^2023.7.22"

Expand All @@ -35,7 +33,7 @@ httpx = "^0.24.1"
pytest = "^7.4.1"
types-requests = "^2.31.0.1"
pre-commit = "^3.4.0"
chromadb = "^0.3.2.6" # required for ex8.py
chromadb = "^0.3.2.6" # required for ex8.py

[tool.ruff]
line-length = 120
Expand Down
17 changes: 17 additions & 0 deletions tests.http
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,20 @@ Content-Type: application/json
{}
]
}

###

POST http://0.0.0.0:8718/examples.ex14.chain/run
X-LLM-API-KEY: sk-invalid-key
Content-Type: application/json

{
"chat_history": [[
"string",
"string"
]],
"question": "string",
"memory": [
{}
]
}

1 comment on commit 17f804e

@vercel
Copy link

@vercel vercel bot commented on 17f804e Sep 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

langcorn – ./

langcorn.vercel.app
langcorn-msoedov.vercel.app
langcorn-git-main-msoedov.vercel.app

Please sign in to comment.