Giter VIP home page Giter VIP logo

chat-langchain's Introduction

🦜️🔗 Chat LangChain

This repo is an implementation of a locally hosted chatbot specifically focused on question answering over the LangChain documentation. Built with LangChain, FastAPI, and Next.js.

Deployed version: chat.langchain.com

Looking for the JS version? Click here.

The app leverages LangChain's streaming support and async API to update the page in real time for multiple users.

✅ Running locally

  1. Install backend dependencies: poetry install.
  2. Make sure to enter your environment variables to configure the application:
export OPENAI_API_KEY=
export WEAVIATE_URL=
export WEAVIATE_API_KEY=
export RECORD_MANAGER_DB_URL=

# for tracing
export LANGCHAIN_TRACING_V2=true
export LANGCHAIN_ENDPOINT="https://api.smith.langchain.com"
export LANGCHAIN_API_KEY=
export LANGCHAIN_PROJECT=
  1. Run python backend/ingest.py to ingest LangChain docs data into the Weaviate vectorstore (only needs to be done once).
    1. You can use other Document Loaders to load your own data into the vectorstore.
  2. Start the Python backend with make start.
  3. Install frontend dependencies by running cd ./frontend, then yarn.
  4. Run the frontend with yarn dev for frontend.
  5. Open localhost:3000 in your browser.

📚 Technical description

There are two components: ingestion and question-answering.

Ingestion has the following steps:

  1. Pull html from documentation site as well as the Github Codebase
  2. Load html with LangChain's RecursiveURLLoader and SitemapLoader
  3. Split documents with LangChain's RecursiveCharacterTextSplitter
  4. Create a vectorstore of embeddings, using LangChain's Weaviate vectorstore wrapper (with OpenAI's embeddings).

Question-Answering has the following steps:

  1. Given the chat history and new user input, determine what a standalone question would be using GPT-3.5.
  2. Given that standalone question, look up relevant documents from the vectorstore.
  3. Pass the standalone question and relevant documents to the model to generate and stream the final answer.
  4. Generate a trace URL for the current chat session, as well as the endpoint to collect feedback.

Documentation

Looking to use or modify this Use Case Accelerant for your own needs? We've added a few docs to aid with this:

  • Concepts: A conceptual overview of the different components of Chat LangChain. Goes over features like ingestion, vector stores, query analysis, etc.
  • Modify: A guide on how to modify Chat LangChain for your own needs. Covers the frontend, backend and everything in between.
  • Running Locally: The steps to take to run Chat LangChain 100% locally.
  • LangSmith: A guide on adding robustness to your application using LangSmith. Covers observability, evaluations, and feedback.
  • Production: Documentation on preparing your application for production usage. Explains different security considerations, and more.
  • Deployment: How to deploy your application to production. Covers setting up production databases, deploying the frontend, and more.

chat-langchain's People

Contributors

agola11 avatar baskaryan avatar beatrixcohere avatar bracesproul avatar efriis avatar eltociear avatar eyurtsev avatar hinthornw avatar hwchase17 avatar jacoblee93 avatar jkilpatrick1 avatar jvelezmagic avatar langchain-infra avatar mcantillon21 avatar nfcampos avatar raceronskis avatar samnoyes avatar sangyh avatar sid77x avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

chat-langchain's Issues

3MB txt failed FAISS.from_documents(). openai limit met?can I divide &conquer big files to get the vectorstore?

codes:
from dotenv import load_dotenv
load_dotenv()

from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain.document_loaders import UnstructuredFileLoader
from langchain.vectorstores.faiss import FAISS
from langchain.embeddings import OpenAIEmbeddings
import pickle

Load Data

loader = UnstructuredFileLoader("a.txt")
raw_documents = loader.load()

Split text

text_splitter = RecursiveCharacterTextSplitter()
documents = text_splitter.split_documents(raw_documents)

Load Data to vectorstore

embeddings = OpenAIEmbeddings()
vectorstore = FAISS.from_documents(documents, embeddings)


error:

(base) PS F:\test> python .\inges.py
D:\miniconda3\lib\site-packages\requests_init_.py:102: RequestsDependencyWarning: urllib3 (1.26.8) or chardet (5.1.0)/charset_normalizer (2.1.1) doesn't match a supported version!
warnings.warn("urllib3 ({}) or chardet ({})/charset_normalizer ({}) doesn't match a supported "
Traceback (most recent call last):
File "F:\test\ingest.py", line 22, in
raw_documents = loader.load()
File "D:\miniconda3\lib\site-packages\langchain\document_loaders\unstructured.py", line 38, in load
elements = self.get_elements()
File "D:\miniconda3\lib\site-packages\langchain\document_loaders\unstructured.py", line 70, in get_elements
return partition(filename=self.file_path)
File "D:\miniconda3\lib\site-packages\unstructured\partition\auto.py", line 58, in partition
return partition_text(filename=filename, file=file)
File "D:\miniconda3\lib\site-packages\unstructured\partition\text.py", line 41, in partition_text
file_text = f.read()
File "D:\miniconda3\lib\codecs.py", line 322, in decode
(result, consumed) = self.buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte
(base) PS F:\test> python .\ingestNeovim.py
D:\miniconda3\lib\site-packages\requests_init
.py:102: RequestsDependencyWarning: urllib3 (1.26.8) or chardet (5.1.0)/charset_normalizer (2.1.1) doesn't match a supported version!
warnings.warn("urllib3 ({}) or chardet ({})/charset_normalizer ({}) doesn't match a supported "
(base) PS F:\test> python .\ingestNeovim.py >1.log
D:\miniconda3\lib\site-packages\requests_init
.py:102: RequestsDependencyWarning: urllib3 (1.26.8) or chardet (5.1.0)/charset_normalizer (2.1.1) doesn't match a supported version!
warnings.warn("urllib3 ({}) or chardet ({})/charset_normalizer ({}) doesn't match a supported "
(base) PS F:\test> python .\ingestNeovim.py
D:\miniconda3\lib\site-packages\requests_init
.py:102: RequestsDependencyWarning: urllib3 (1.26.8) or chardet (5.1.0)/charset_normalizer (2.1.1) doesn't match a supported version!
warnings.warn("urllib3 ({}) or chardet ({})/charset_normalizer ({}) doesn't match a supported "
Retrying langchain.embeddings.openai.embed_with_retry.._completion_with_retry in 4.0 seconds as it raised APIError: Internal error {
"error": {
"message": "Internal error",
"type": "internal_error",
"param": null,
"code": "internal_error"
}
}
500 {'error': {'message': 'Internal error', 'type': 'internal_error', 'param': None, 'code': 'internal_error'}} {'Date': 'Sat, 11 Mar 2023 12:41:21 GMT', 'Content-Type': 'application/json; charset=utf-8', 'Content-Length': '152', 'Connection': 'keep-alive', 'Vary': 'Origin', 'X-Request-Id': 'c3d10e2343a2b3408ed03a5c4ffe61a3', 'Strict-Transport-Security': 'max-age=15724800; includeSubDomains'}.
Retrying langchain.embeddings.openai.embed_with_retry.._completion_with_retry in 4.0 seconds as it raised APIError: Internal error {
"error": {
"message": "Internal error",
"type": "internal_error",
"param": null,
"code": "internal_error"
}
}
500 {'error': {'message': 'Internal error', 'type': 'internal_error', 'param': None, 'code': 'internal_error'}} {'Date': 'Sat, 11 Mar 2023 12:42:18 GMT', 'Content-Type': 'application/json; charset=utf-8', 'Content-Length': '152', 'Connection': 'keep-alive', 'Vary': 'Origin', 'X-Request-Id': '8ad519d0b7c0acac76dff60931b41a86', 'Strict-Transport-Security': 'max-age=15724800; includeSubDomains'}.
Retrying langchain.embeddings.openai.embed_with_retry..completion_with_retry in 4.0 seconds as it raised APIError: Internal error {
"error": {
"message": "Internal error",
"type": "internal_error",
"param": null,
"code": "internal_error"
}
}
500 {'error': {'message': 'Internal error', 'type': 'internal_error', 'param': None, 'code': 'internal_error'}} {'Date': 'Sat, 11 Mar 2023 12:43:02 GMT', 'Content-Type': 'application/json; charset=utf-8', 'Content-Length': '152', 'Connection': 'keep-alive', 'Vary': 'Origin', 'X-Request-Id': '9e4d69bd26627466aff64ef69b1379cf', 'Strict-Transport-Security': 'max-age=15724800; includeSubDomains'}.
Retrying langchain.embeddings.openai.embed_with_retry..completion_with_retry in 8.0 seconds as it raised APIError: Internal error {
"error": {
"message": "Internal error",
"type": "internal_error",
"param": null,
"code": "internal_error"
}
}
500 {'error': {'message': 'Internal error', 'type': 'internal_error', 'param': None, 'code': 'internal_error'}} {'Date': 'Sat, 11 Mar 2023 12:43:49 GMT', 'Content-Type': 'application/json; charset=utf-8', 'Content-Length': '152', 'Connection': 'keep-alive', 'Vary': 'Origin', 'X-Request-Id': '1fce29a7209b4fe29836a86db29ab6ae', 'Strict-Transport-Security': 'max-age=15724800; includeSubDomains'}.
Retrying langchain.embeddings.openai.embed_with_retry..completion_with_retry in 10.0 seconds as it raised APIError: Internal error {
"error": {
"message": "Internal error",
"type": "internal_error",
"param": null,
"code": "internal_error"
}
}
500 {'error': {'message': 'Internal error', 'type': 'internal_error', 'param': None, 'code': 'internal_error'}} {'Date': 'Sat, 11 Mar 2023 12:44:50 GMT', 'Content-Type': 'application/json; charset=utf-8', 'Content-Length': '152', 'Connection': 'keep-alive', 'Vary': 'Origin', 'X-Request-Id': 'f10ec050ead37c907556b22b91fd888e', 'Strict-Transport-Security': 'max-age=15724800; includeSubDomains'}.
Traceback (most recent call last):
File "F:\test\ingestNeovim.py", line 31, in
vectorstore = FAISS.from_documents(documents, embeddings)
File "D:\miniconda3\lib\site-packages\langchain\vectorstores\base.py", line 113, in from_documents
return cls.from_texts(texts, embedding, metadatas=metadatas, **kwargs)
File "D:\miniconda3\lib\site-packages\langchain\vectorstores\faiss.py", line 250, in from_texts
embeddings = embedding.embed_documents(texts)
File "D:\miniconda3\lib\site-packages\langchain\embeddings\openai.py", line 203, in embed_documents
response = embed_with_retry(
File "D:\miniconda3\lib\site-packages\langchain\embeddings\openai.py", line 53, in embed_with_retry
return completion_with_retry(**kwargs)
File "D:\miniconda3\lib\site-packages\tenacity_init
.py", line 289, in wrapped_f
return self(f, *args, **kw)
File "D:\miniconda3\lib\site-packages\tenacity_init
.py", line 379, in call
do = self.iter(retry_state=retry_state)
File "D:\miniconda3\lib\site-packages\tenacity_init.py", line 325, in iter
raise retry_exc.reraise()
File "D:\miniconda3\lib\site-packages\tenacity_init.py", line 158, in reraise
raise self.last_attempt.result()
File "D:\miniconda3\lib\concurrent\futures_base.py", line 451, in result
return self.__get_result()
File "D:\miniconda3\lib\concurrent\futures_base.py", line 403, in __get_result
raise self.exception
File "D:\miniconda3\lib\site-packages\tenacity_init
.py", line 382, in call
result = fn(*args, **kwargs)
File "D:\miniconda3\lib\site-packages\langchain\embeddings\openai.py", line 51, in _completion_with_retry
return embeddings.client.create(**kwargs)
File "D:\miniconda3\lib\site-packages\openai\api_resources\embedding.py", line 34, in create
response = super().create(*args, **kwargs)
File "D:\miniconda3\lib\site-packages\openai\api_resources\abstract\engine_api_resource.py", line 115, in create
response, _, api_key = requestor.request(
File "D:\miniconda3\lib\site-packages\openai\api_requestor.py", line 181, in request
resp, got_stream = self._interpret_response(result, stream)
File "D:\miniconda3\lib\site-packages\openai\api_requestor.py", line 396, in _interpret_response
self._interpret_response_line(
File "D:\miniconda3\lib\site-packages\openai\api_requestor.py", line 429, in _interpret_response_line
raise self.handle_error_response(
openai.error.APIError: Internal error {
"error": {
"message": "Internal error",
"type": "internal_error",
"param": null,
"code": "internal_error"
}
}
500 {'error': {'message': 'Internal error', 'type': 'internal_error', 'param': None, 'code': 'internal_error'}} {'Date': 'Sat, 11 Mar 2023 12:45:47 GMT', 'Content-Type': 'application/json; charset=utf-8', 'Content-Length': '152', 'Connection': 'keep-alive', 'Vary': 'Origin', 'X-Request-Id': '39e42631e12228f19c485055b24bb8fe', 'Strict-Transport-Security': 'max-age=15724800; includeSubDomains'}

Error communicating with OpenAI

OS: Mac OS M1

During setup project, i've faced with connection problem with Open AI.

My steps to repeat:

1. langchain-server

In iterm2 terminal

>export OPENAI_API_KEY=sk-K6E****
>langchain-server

logs

[+] Running 3/3
 ⠿ langchain-db Pulled                                                                                                                                                                                      1.6s
 ⠿ langchain-frontend Pulled                                                                                                                                                                                1.5s
 ⠿ langchain-backend Pulled                                                                                                                                                                                 1.6s
[+] Running 3/0
 ⠿ Container langchain-langchain-db-1        Created                                                                                                                                                        0.0s
 ⠿ Container langchain-langchain-backend-1   Created                                                                                                                                                        0.0s
 ⠿ Container langchain-langchain-frontend-1  Created                                                                                                                                                        0.0s
Attaching to langchain-langchain-backend-1, langchain-langchain-db-1, langchain-langchain-frontend-1
langchain-langchain-db-1        |
langchain-langchain-db-1        | PostgreSQL Database directory appears to contain a database; Skipping initialization
langchain-langchain-db-1        |
langchain-langchain-db-1        | 2023-02-16 00:45:55.941 UTC [1] LOG:  starting PostgreSQL 14.1 (Debian 14.1-1.pgdg110+1) on aarch64-unknown-linux-gnu, compiled by gcc (Debian 10.2.1-6) 10.2.1 20210110, 64-bit
langchain-langchain-db-1        | 2023-02-16 00:45:55.941 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432
langchain-langchain-db-1        | 2023-02-16 00:45:55.941 UTC [1] LOG:  listening on IPv6 address "::", port 5432
langchain-langchain-db-1        | 2023-02-16 00:45:55.946 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
langchain-langchain-db-1        | 2023-02-16 00:45:55.951 UTC [26] LOG:  database system was shut down at 2023-02-16 00:44:38 UTC
langchain-langchain-db-1        | 2023-02-16 00:45:55.957 UTC [1] LOG:  database system is ready to accept connections
langchain-langchain-backend-1   | /code/./app/config.py:106: UserWarning: directory "/run/secrets" does not exist
langchain-langchain-backend-1   |   settings = Settings(LANGCHAIN_ENV=LANGCHAIN_ENV, _env_file=f"app/.env.{LANGCHAIN_ENV}")
langchain-langchain-frontend-1  |
langchain-langchain-frontend-1  | > [email protected] preview
langchain-langchain-frontend-1  | > vite preview --host
langchain-langchain-frontend-1  |
langchain-langchain-backend-1   | INFO:     Started server process [1]
langchain-langchain-backend-1   | INFO:     Waiting for application startup.
langchain-langchain-backend-1   | INFO:     Application startup complete.
langchain-langchain-backend-1   | INFO:     Uvicorn running on http://0.0.0.0:8000 (Press CTRL+C to quit)
langchain-langchain-frontend-1  |
langchain-langchain-frontend-1  | PUBLIC_BASE_URL is set to:  http://localhost:8000
langchain-langchain-frontend-1  | DEV_MODE is set to:  true
langchain-langchain-frontend-1  |   ➜  Local:   http://localhost:4173/
langchain-langchain-frontend-1  |   ➜  Network: http://172.19.0.4:4173/
langchain-langchain-backend-1   | INFO:     172.19.0.4:42116 - "GET /sessions HTTP/1.1" 200 OK

2. Chat

In separate tab, I am trying to run chat

chat-langchain % export OPENAI_API_KEY=sk-***
pip3 install -r requirements.txt

Logs

Requirement already satisfied: openai in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from -r requirements.txt (line 1)) (0.26.5)
Requirement already satisfied: fastapi in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from -r requirements.txt (line 2)) (0.92.0)
Requirement already satisfied: black in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from -r requirements.txt (line 3)) (23.1.0)
Requirement already satisfied: isort in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from -r requirements.txt (line 4)) (5.12.0)
Requirement already satisfied: websockets in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from -r requirements.txt (line 5)) (10.4)
Requirement already satisfied: pydantic in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from -r requirements.txt (line 6)) (1.10.4)
Requirement already satisfied: langchain in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from -r requirements.txt (line 7)) (0.0.87)
Requirement already satisfied: uvicorn in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from -r requirements.txt (line 8)) (0.20.0)
Requirement already satisfied: jinja2 in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from -r requirements.txt (line 9)) (3.1.2)
Requirement already satisfied: faiss-cpu in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from -r requirements.txt (line 10)) (1.7.3)
Requirement already satisfied: bs4 in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from -r requirements.txt (line 11)) (0.0.1)
Requirement already satisfied: unstructured in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from -r requirements.txt (line 12)) (0.4.8)
Requirement already satisfied: libmagic in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from -r requirements.txt (line 13)) (1.0)
Requirement already satisfied: requests>=2.20 in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from openai->-r requirements.txt (line 1)) (2.28.2)
Requirement already satisfied: tqdm in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from openai->-r requirements.txt (line 1)) (4.64.1)
Requirement already satisfied: aiohttp in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from openai->-r requirements.txt (line 1)) (3.8.4)
Requirement already satisfied: starlette<0.26.0,>=0.25.0 in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from fastapi->-r requirements.txt (line 2)) (0.25.0)
Requirement already satisfied: click>=8.0.0 in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from black->-r requirements.txt (line 3)) (8.1.3)
Requirement already satisfied: mypy-extensions>=0.4.3 in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from black->-r requirements.txt (line 3)) (1.0.0)
Requirement already satisfied: packaging>=22.0 in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from black->-r requirements.txt (line 3)) (23.0)
Requirement already satisfied: pathspec>=0.9.0 in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from black->-r requirements.txt (line 3)) (0.11.0)
Requirement already satisfied: platformdirs>=2 in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from black->-r requirements.txt (line 3)) (3.0.0)
Requirement already satisfied: typing-extensions>=4.2.0 in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from pydantic->-r requirements.txt (line 6)) (4.5.0)
Requirement already satisfied: PyYAML<7,>=6 in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from langchain->-r requirements.txt (line 7)) (6.0)
Requirement already satisfied: SQLAlchemy<2,>=1 in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from langchain->-r requirements.txt (line 7)) (1.4.46)
Requirement already satisfied: dataclasses-json<0.6.0,>=0.5.7 in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from langchain->-r requirements.txt (line 7)) (0.5.7)
Requirement already satisfied: numpy<2,>=1 in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from langchain->-r requirements.txt (line 7)) (1.23.5)
Requirement already satisfied: tenacity<9.0.0,>=8.1.0 in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from langchain->-r requirements.txt (line 7)) (8.2.1)
Requirement already satisfied: h11>=0.8 in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from uvicorn->-r requirements.txt (line 8)) (0.14.0)
Requirement already satisfied: MarkupSafe>=2.0 in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from jinja2->-r requirements.txt (line 9)) (2.1.2)
Requirement already satisfied: beautifulsoup4 in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from bs4->-r requirements.txt (line 11)) (4.11.2)
Requirement already satisfied: argilla in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from unstructured->-r requirements.txt (line 12)) (1.3.0)
Requirement already satisfied: lxml in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from unstructured->-r requirements.txt (line 12)) (4.9.2)
Requirement already satisfied: nltk in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from unstructured->-r requirements.txt (line 12)) (3.8.1)
Requirement already satisfied: openpyxl in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from unstructured->-r requirements.txt (line 12)) (3.1.1)
Requirement already satisfied: pandas in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from unstructured->-r requirements.txt (line 12)) (1.5.3)
Requirement already satisfied: pillow in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from unstructured->-r requirements.txt (line 12)) (9.4.0)
Requirement already satisfied: python-docx in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from unstructured->-r requirements.txt (line 12)) (0.8.11)
Requirement already satisfied: python-pptx in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from unstructured->-r requirements.txt (line 12)) (0.6.21)
Requirement already satisfied: python-magic in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from unstructured->-r requirements.txt (line 12)) (0.4.27)
Requirement already satisfied: certifi>=2022.12.07 in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from unstructured->-r requirements.txt (line 12)) (2022.12.7)
Requirement already satisfied: attrs>=17.3.0 in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from aiohttp->openai->-r requirements.txt (line 1)) (22.2.0)
Requirement already satisfied: charset-normalizer<4.0,>=2.0 in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from aiohttp->openai->-r requirements.txt (line 1)) (3.0.1)
Requirement already satisfied: multidict<7.0,>=4.5 in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from aiohttp->openai->-r requirements.txt (line 1)) (6.0.4)
Requirement already satisfied: async-timeout<5.0,>=4.0.0a3 in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from aiohttp->openai->-r requirements.txt (line 1)) (4.0.2)
Requirement already satisfied: yarl<2.0,>=1.0 in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from aiohttp->openai->-r requirements.txt (line 1)) (1.8.2)
Requirement already satisfied: frozenlist>=1.1.1 in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from aiohttp->openai->-r requirements.txt (line 1)) (1.3.3)
Requirement already satisfied: aiosignal>=1.1.2 in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from aiohttp->openai->-r requirements.txt (line 1)) (1.3.1)
Requirement already satisfied: marshmallow<4.0.0,>=3.3.0 in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from dataclasses-json<0.6.0,>=0.5.7->langchain->-r requirements.txt (line 7)) (3.19.0)
Requirement already satisfied: marshmallow-enum<2.0.0,>=1.5.1 in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from dataclasses-json<0.6.0,>=0.5.7->langchain->-r requirements.txt (line 7)) (1.5.1)
Requirement already satisfied: typing-inspect>=0.4.0 in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from dataclasses-json<0.6.0,>=0.5.7->langchain->-r requirements.txt (line 7)) (0.8.0)
Requirement already satisfied: idna<4,>=2.5 in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from requests>=2.20->openai->-r requirements.txt (line 1)) (3.4)
Requirement already satisfied: urllib3<1.27,>=1.21.1 in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from requests>=2.20->openai->-r requirements.txt (line 1)) (1.26.14)
Requirement already satisfied: anyio<5,>=3.4.0 in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from starlette<0.26.0,>=0.25.0->fastapi->-r requirements.txt (line 2)) (3.6.2)
Requirement already satisfied: httpx<0.24,>=0.15 in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from argilla->unstructured->-r requirements.txt (line 12)) (0.23.3)
Requirement already satisfied: deprecated~=1.2.0 in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from argilla->unstructured->-r requirements.txt (line 12)) (1.2.13)
Requirement already satisfied: wrapt<1.15,>=1.13 in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from argilla->unstructured->-r requirements.txt (line 12)) (1.14.1)
Requirement already satisfied: backoff in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from argilla->unstructured->-r requirements.txt (line 12)) (2.2.1)
Requirement already satisfied: monotonic in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from argilla->unstructured->-r requirements.txt (line 12)) (1.6)
Requirement already satisfied: python-dateutil>=2.8.1 in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from pandas->unstructured->-r requirements.txt (line 12)) (2.8.2)
Requirement already satisfied: pytz>=2020.1 in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from pandas->unstructured->-r requirements.txt (line 12)) (2022.7.1)
Requirement already satisfied: soupsieve>1.2 in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from beautifulsoup4->bs4->-r requirements.txt (line 11)) (2.4)
Requirement already satisfied: joblib in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from nltk->unstructured->-r requirements.txt (line 12)) (1.2.0)
Requirement already satisfied: regex>=2021.8.3 in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from nltk->unstructured->-r requirements.txt (line 12)) (2022.10.31)
Requirement already satisfied: et-xmlfile in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from openpyxl->unstructured->-r requirements.txt (line 12)) (1.1.0)
Requirement already satisfied: XlsxWriter>=0.5.7 in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from python-pptx->unstructured->-r requirements.txt (line 12)) (3.0.8)
Requirement already satisfied: sniffio>=1.1 in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from anyio<5,>=3.4.0->starlette<0.26.0,>=0.25.0->fastapi->-r requirements.txt (line 2)) (1.3.0)
Requirement already satisfied: httpcore<0.17.0,>=0.15.0 in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from httpx<0.24,>=0.15->argilla->unstructured->-r requirements.txt (line 12)) (0.16.3)
Requirement already satisfied: rfc3986[idna2008]<2,>=1.3 in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from httpx<0.24,>=0.15->argilla->unstructured->-r requirements.txt (line 12)) (1.5.0)
Requirement already satisfied: six>=1.5 in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from python-dateutil>=2.8.1->pandas->unstructured->-r requirements.txt (line 12)) (1.16.0)

then, I am running ingest.sh

chat-langchain % sh ./ingest.sh

image

by the end, these lines

--2023-02-16 01:54:04--  https://langchain.readthedocs.io/en/latest/tracing/agent_with_tracing.html
Reusing existing connection to langchain.readthedocs.io:443.
HTTP request sent, awaiting response... 200 OK
Length: unspecified [text/html]
Saving to: ‘langchain.readthedocs.io/en/latest/tracing/agent_with_tracing.html’

langchain.readthedocs.io/en/latest/tracing/agent_wit     [ <=>                                                                                                                ]  50,68K  --.-KB/s    in 0,004s

2023-02-16 01:54:04 (12,1 MB/s) - ‘langchain.readthedocs.io/en/latest/tracing/agent_with_tracing.html’ saved [51892]

FINISHED --2023-02-16 01:54:04--
Total wall clock time: 2m 9s
Downloaded: 263 files, 16M in 0,6s (28,4 MB/s)
/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/langchain/document_loaders/readthedocs.py:21: GuessedAtParserWarning: No parser was explicitly specified, so I'm using the best available HTML parser for this system ("lxml"). This usually isn't a problem, but if you run this code on another system, or in a different virtual environment, it may use a different parser and behave differently.

The code that caused this warning is on line 21 of the file /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/langchain/document_loaders/readthedocs.py. To get rid of this warning, pass the additional argument 'features="lxml"' to the BeautifulSoup constructor.

  soup = BeautifulSoup(data)

3. make start

>make start

logs

uvicorn main:app --reload --port 9000
INFO:     Will watch for changes in these directories: ['/Users/dmowski/Desktop/ai/chat-langchain']
INFO:     Uvicorn running on http://127.0.0.1:9000 (Press CTRL+C to quit)
INFO:     Started reloader process [19796] using StatReload
INFO:     Started server process [19798]
INFO:     Waiting for application startup.
INFO:     Application startup complete.

4. Trying to open http://127.0.0.1:9000

image

image

image

image

After few minutes

image

logs

uvicorn main:app --reload --port 9000
INFO:     Will watch for changes in these directories: ['/Users/dmowski/Desktop/ai/chat-langchain']
INFO:     Uvicorn running on http://127.0.0.1:9000 (Press CTRL+C to quit)
INFO:     Started reloader process [19796] using StatReload
INFO:     Started server process [19798]
INFO:     Waiting for application startup.
INFO:     Application startup complete.
INFO:     127.0.0.1:53719 - "GET / HTTP/1.1" 200 OK
INFO:     ('127.0.0.1', 53722) - "WebSocket /chat" [accepted]
INFO:     connection open
WARNING:langchain.llms.openai:Retrying langchain.llms.openai.BaseOpenAI.acompletion_with_retry.<locals>._completion_with_retry in 4.0 seconds as it raised APIConnectionError: Error communicating with OpenAI.
WARNING:langchain.llms.openai:Retrying langchain.llms.openai.BaseOpenAI.acompletion_with_retry.<locals>._completion_with_retry in 4.0 seconds as it raised APIConnectionError: Error communicating with OpenAI.
WARNING:langchain.llms.openai:Retrying langchain.llms.openai.BaseOpenAI.acompletion_with_retry.<locals>._completion_with_retry in 4.0 seconds as it raised APIConnectionError: Error communicating with OpenAI.
WARNING:langchain.llms.openai:Retrying langchain.llms.openai.BaseOpenAI.acompletion_with_retry.<locals>._completion_with_retry in 8.0 seconds as it raised APIConnectionError: Error communicating with OpenAI.
WARNING:langchain.llms.openai:Retrying langchain.llms.openai.BaseOpenAI.acompletion_with_retry.<locals>._completion_with_retry in 10.0 seconds as it raised APIConnectionError: Error communicating with OpenAI.
ERROR:root:Error communicating with OpenAI

Logs from http://localhost:4173/session-1

image


Also I am able to call OpenAI api (In separate script)

image


image

Running into error ERROR:root:type object 'Completion' has no attribute 'acreate'

Not sure if this is an issue or some local issue at my end but I modified the ingest.py to ingest DirectoryLoader and feeding in .md file from the local GitHub repo and validated that the vectorstore.pkl is created and populated. But when I run the chat bot and post any question in the window I observer ERROR:root:type object 'Completion' has no attribute 'acreate'

Any help or pointer around this is appreciated.

Why can't my chat generate code

I deployed chat correctly, but its answer was a bit clumsy, and it was not as good as my direct 'ctrl+f' search. And it can't generate code anyway

Implement agent

How would I need to go about to add an agent (such as llm-math) to this solution?

Thanks for the great work btw, this really helped me to understand a lot more about langchain!

How to integrate ConversationalRetrievalChain with Custom Agent returning the source documents

I am using the latest version of langchain to build chat with data application.

I want to use Agent with ConversationalRetrievalChain which can return source documents. Getting following error

My code is:
qa_ret = ConversationalRetrievalChain.from_llm(ChatOpenAI(temperature=0), vectorstore.as_retriever(), qa_prompt=prompt, return_source_documents=True)

tools = [
Tool(
name = "Docstore",
func=qa_ret.run,
description="useful for when you need to answer questions about document",
return_direct=True
),
]

agent = initialize_agent(tools, llm, agent="zero-shot-react-description", verbose=False)

agent.run("What is Apple?")

Error:

ValueError Traceback (most recent call last)
Cell In[51], line 1
----> 1 response = agent.run("What is Apple?")

~/.conda/envs/langChain2/lib/python3.10/site-packages/langchain/chains/base.py:213), in Chain.run(self, *args, **kwargs)
211 if len(args) != 1:
212 raise ValueError("run supports only one positional argument.")
--> 213 return self(args[0])[self.output_keys[0]]
215 if kwargs and not args:
216 return self(kwargs)[self.output_keys[0]]

~/.conda/envs/langChain2/lib/python3.10/site-packages/langchain/chains/base.py:116), in Chain.call(self, inputs, return_only_outputs)
114 except (KeyboardInterrupt, Exception) as e:
115 self.callback_manager.on_chain_error(e, verbose=self.verbose)
--> 116 raise e
117 self.callback_manager.on_chain_end(outputs, verbose=self.verbose)
118 return self.prep_outputs(inputs, outputs, return_only_outputs)

~/.conda/envs/langChain2/lib/python3.10/site-packages/langchain/chains/base.py:113), in Chain.call(self, inputs, return_only_outputs)
107 self.callback_manager.on_chain_start(
108 {"name": self.class.name},
109 inputs,
110 verbose=self.verbose,
111 )
...
208 )
210 if args and not kwargs:
211 if len(args) != 1:

ValueError: run not supported when there is not exactly one output key. Got ['answer', 'source_documents'].

The web ui hangs after chat input, and console print message of 405 Method Not Allowed

(venv) root@vps:~/mdev/chat-langchain# make start
uvicorn main:app --reload --port 9000 --host 0.0.0.0 INFO: Will watch for changes in these directories: ['/root/mdev/chat-langchain'] INFO: Uvicorn running on http://0.0.0.0:9000 (Press CTRL+C to quit)
INFO: Started reloader process [946838] using StatReload
INFO: Started server process [946840]
INFO: Waiting for application startup.
INFO: Application startup complete.
INFO: :54010 - "GET / HTTP/1.1" 200 OK
INFO: :54010 - "HEAD / HTTP/1.1" 405 Method Not Allowed
INFO: :54010 - "GET /favicon.ico HTTP/1.1" 404 Not Found

Missing license

Hi Harrison,
I like the chat code you put together, but there is no license attached. Could you please consider adding one to this repository?

Cheers,
Martin

Loader issue

I'm trying to replicate the steps here 'https://github.com/hwchase17/chat-langchain' and while running ingest.sh, I get the following error: No module named 'langchain.document_loaders'. Any ideas on how best to resolve this would be appreciated. Thanks in advance!

What does the archive folder contain exactly?

Hi, I was going through the repository, and I am having a hard time understanding what the code in the archive folder does exactly (or whether it is used at all?). The prompt mentioned in the blog is only there in the archive folder, so it must be called somewhere right?

KeyError: 'data` after the first successful answer

I'm seeing a constant error for all questions after it answered the first question,

==== date/time: 2023-01-28 15:58:56.838016 ====
inp: What are agents?
Traceback (most recent call last):
  File "/Users/ziscore/Work/2023/chat-langchain/venv/lib/python3.8/site-packages/gradio/routes.py", line 337, in run_predict
    output = await app.get_blocks().process_api(
  File "/Users/ziscore/Work/2023/chat-langchain/venv/lib/python3.8/site-packages/gradio/blocks.py", line 1015, in process_api
    result = await self.call_function(
  File "/Users/ziscore/Work/2023/chat-langchain/venv/lib/python3.8/site-packages/gradio/blocks.py", line 833, in call_function
    prediction = await anyio.to_thread.run_sync(
  File "/Users/ziscore/Work/2023/chat-langchain/venv/lib/python3.8/site-packages/anyio/to_thread.py", line 31, in run_sync
    return await get_asynclib().run_sync_in_worker_thread(
  File "/Users/ziscore/Work/2023/chat-langchain/venv/lib/python3.8/site-packages/anyio/_backends/_asyncio.py", line 937, in run_sync_in_worker_thread
    return await future
  File "/Users/ziscore/Work/2023/chat-langchain/venv/lib/python3.8/site-packages/anyio/_backends/_asyncio.py", line 867, in run
    result = context.run(func, *args)
  File "app.py", line 39, in chat
    output = agent({"question": inp, "chat_history": history})
  File "/Users/ziscore/Work/2023/chat-langchain/venv/lib/python3.8/site-packages/langchain/chains/base.py", line 146, in __call__
    raise e
  File "/Users/ziscore/Work/2023/chat-langchain/venv/lib/python3.8/site-packages/langchain/chains/base.py", line 142, in __call__
    outputs = self._call(inputs)
  File "/Users/ziscore/Work/2023/chat-langchain/chain.py", line 39, in _call
    new_question = self.key_word_extractor.run(
  File "/Users/ziscore/Work/2023/chat-langchain/venv/lib/python3.8/site-packages/langchain/chains/base.py", line 175, in run
    return self(kwargs)[self.output_keys[0]]
  File "/Users/ziscore/Work/2023/chat-langchain/venv/lib/python3.8/site-packages/langchain/chains/base.py", line 146, in __call__
    raise e
  File "/Users/ziscore/Work/2023/chat-langchain/venv/lib/python3.8/site-packages/langchain/chains/base.py", line 142, in __call__
    outputs = self._call(inputs)
  File "/Users/ziscore/Work/2023/chat-langchain/venv/lib/python3.8/site-packages/langchain/chains/llm.py", line 87, in _call
    return self.apply([inputs])[0]
  File "/Users/ziscore/Work/2023/chat-langchain/venv/lib/python3.8/site-packages/langchain/chains/llm.py", line 78, in apply
    response = self.generate(input_list)
  File "/Users/ziscore/Work/2023/chat-langchain/venv/lib/python3.8/site-packages/langchain/chains/llm.py", line 63, in generate
    prompt = self.prompt.format(**selected_inputs)
  File "/Users/ziscore/Work/2023/chat-langchain/venv/lib/python3.8/site-packages/langchain/prompts/few_shot.py", line 101, in format
    examples = self._get_examples(**kwargs)
  File "/Users/ziscore/Work/2023/chat-langchain/venv/lib/python3.8/site-packages/langchain/prompts/few_shot.py", line 81, in _get_examples
    return self.example_selector.select_examples(kwargs)
  File "/Users/ziscore/Work/2023/chat-langchain/venv/lib/python3.8/site-packages/langchain/prompts/example_selector/semantic_similarity.py", line 44, in select_examples
    example_docs = self.vectorstore.similarity_search(query, k=self.k)
  File "/Users/ziscore/Work/2023/chat-langchain/venv/lib/python3.8/site-packages/langchain/vectorstores/weaviate.py", line 80, in similarity_search
    for res in result["data"]["Get"][self._index_name]:
KeyError: 'data'

Is there a way to do it with SSE (Server Sent Events) rather than Websockets?

Hi, this is very useful and inspiring example, but in my case I need to use one way communication using SSE, and does anybody have a guidance how to implement SSE for chains?

I can see LLMs (OpenAI) has stream() method which returns a generator and using that it is easy to implement SSE using Fast API (https://stackoverflow.com/a/75851218/6624291) however I don't see any way Chains return a generator for each token.

Any help is appreciated. Thanks

Add system prompt

It will be great to have a example on how to add a system prompt to the chain.

ERROR:root:ChatVectorDBChain does not support async

INFO: ('127.0.0.1', 58212) - "WebSocket /chat" [accepted]
/Users/narendra/Desktop/Metadome/chat-langchain/chatgpt/lib/python3.10/site-packages/langchain/chains/conversational_retrieval/base.py:191: UserWarning: ChatVectorDBChain is deprecated - please use from langchain.chains import ConversationalRetrievalChain
warnings.warn(
INFO: connection open
ERROR:root:ChatVectorDBChain does not support async

Unable to run: `ChatVectorDBChain` is deprecated - please use `from langchain.chains import ConversationalRetrievalChain`

After issuing a query in the browser the following error is thrown in the console.

/anaconda3/envs/chat-langchain/lib/python3.9/site-packages/langchain/chains/conversational_retrieval/base.py:191: UserWarning: ChatVectorDBChain is deprecated - please use from langchain.chains import ConversationalRetrievalChain

Looks like an update is needed. I tried making some edits but failed.
Anyone able to fix this?

Wish this could be more like talking with GPT4 with hyperlink references to the relevant docs

I guess there's a questionmark over whether this app will be maintained going forward, but what I'd really like to see is that it would be more like talking with GPT4 with hyperlink references to the relevant docs.

Here's what I get locally once I've added in @floomby's fix:

Screenshot 2023-04-21 at 12 41 14

What I'd love to see is an example code suggestion - which I can get from GPT4 if I provide some context, but GPT4 gets wrong cos it doesn't have enough context, but what I'd like to see is something like this:

Here's how you could use RetrievalQAWithSourcesChain with a WebBaseLoader

from langchain.chains import RetrievalQAWithSourcesChain
from langchain.document_loaders import WebBaseLoader

url = "https://python.langchain.com/en/latest/"
loader = WebBaseLoader(url)
chain = RetrievalQAWithSourcesChain.from_chain_type(loader, chain_type="stuff", verbose=True, return_source_documents=True, type="stuff")

chain({"question": "what's the best way to host a langchain?"}, return_only_outputs=True)

And this comes from the example in URL_REF1. Please also refer to URL_REF2 and URL_REF3

Which is sort of what Bing does (actually code above from Bing, which is also wrong ...)

ImportError: cannot import name 'AsyncCallbackManager' from 'langchain.callbacks.base' (/usr/local/Python-3.11.3/lib/python3.11/site-packages/langchain/callbacks/base.py)

when i run ‘make start’ command ,i meet the error ‘ImportError: cannot import name 'AsyncCallbackManager' from 'langchain.callbacks.base' (/usr/local/Python-3.11.3/lib/python3.11/site-packages/langchain/callbacks/base.py)’ ,
i have already install the langchain and make the path correct
is that 'AsyncCallbackManager' has removed or what can i do to sovle the problem ?thx for help

Return example code from the docs

Really cool project! Thanks for open sourcing.

I'm wondering if it can be easily modified to return example code snippets straight from the docs, rather than pointing me to the docs each time.

CleanShot 2023-01-22 at 14 19 19@2x

For questions that can be answered by text only, the chat experience is great. But if you are looking for a code snippet, then the experience of Ask question -> get link to docs -> click on link is more cumbersome than just searching directly in the docs.

Error when running ingest.py

I got an error as below when running "python ingest.py"
langchain/vectorstores/faiss.py", line 251, in from_texts
index = faiss.IndexFlatL2(len(embeddings[0]))
IndexError: list index out of range

Could you please help on it, thanks!

Help / Support with using Azure OpenAI deployment

Hello!

First, I just want to say I have been super impressed with the performance of langchain as well as all of the documentation.
I am however having trouble switching this chat bot over to using an Azure deployment. I am not quite sure what to switch and replace. I am able to get a streaming response working while just testing on its own with

openai.api_type = "azure"
openai.api_base = os.getenv("OPENAI_API_BASE")
openai.api_version = "2023-03-15-preview"
openai.api_key = os.getenv("OPENAI_API_KEY")

chat = AzureChatOpenAI(
    streaming=True,
    callback_manager=CallbackManager([StreamingStdOutCallbackHandler()]),
    verbose=True,
    temperature=0,
    openai_api_version="2023-03-15-preview",
    deployment_name="<deployment-name>",
    openai_api_base="<api-base>"
)
resp = chat([HumanMessage(content="Write me a song about sparkling water.")])

But how can I translate this into useable code for chat-langchain? I guess my main question revolves around the get_chain function and swapping that out. Any help would be greatly appreciated!

First request hangs and it never completes

Hello, the entire installation process completes alright, vectorstore is created, then I start the server with make start, I go to the browser and enter a query, then it freezes at "loading"
Screenshot_20230215_234428_Chrome

Trying to run ingest.sh but receive illegal option

When I try to run the ingest.sh does it output the following error:
./ingest.sh: 4: set: Illegal option -

Have I misunderstood the instructions? I have run pip install -r requirements.txt before trying to execute the .sh file 😊

Outdated

This here is a valuable resource but needs to be updated, I have tried numerous fixes and pull requests from people with no success. langchain is now 0.0.160 and I would love to use this to learn from.

Can someone please update this.

Error: UnicodeEncodeError: 'latin-1' codec can't encode character '\u201c' when calling ingest_docs

OS: Ubuntu 22.04
Python: Python 3.10.6

Traceback (most recent call last):
File "/root/work/chat-langchain/ingest.py", line 35, in
ingest_docs()
File "/root/work/chat-langchain/ingest.py", line 27, in ingest_docs
vectorstore = FAISS.from_documents(documents, embeddings)
File "/usr/local/lib/python3.10/dist-packages/langchain/vectorstores/base.py", line 116, in from_documents
return cls.from_texts(texts, embedding, metadatas=metadatas, **kwargs)
File "/usr/local/lib/python3.10/dist-packages/langchain/vectorstores/faiss.py", line 344, in from_texts
embeddings = embedding.embed_documents(texts)
File "/usr/local/lib/python3.10/dist-packages/langchain/embeddings/openai.py", line 254, in embed_documents
response = embed_with_retry(
File "/usr/local/lib/python3.10/dist-packages/langchain/embeddings/openai.py", line 53, in embed_with_retry
return _completion_with_retry(**kwargs)
File "/usr/local/lib/python3.10/dist-packages/tenacity/init.py", line 289, in wrapped_f
return self(f, *args, **kw)
File "/usr/local/lib/python3.10/dist-packages/tenacity/init.py", line 379, in call
do = self.iter(retry_state=retry_state)
File "/usr/local/lib/python3.10/dist-packages/tenacity/init.py", line 314, in iter
return fut.result()
File "/usr/lib/python3.10/concurrent/futures/_base.py", line 451, in result
return self.__get_result()
File "/usr/lib/python3.10/concurrent/futures/_base.py", line 403, in __get_result
raise self._exception
File "/usr/local/lib/python3.10/dist-packages/tenacity/init.py", line 382, in call
result = fn(*args, **kwargs)
File "/usr/local/lib/python3.10/dist-packages/langchain/embeddings/openai.py", line 51, in _completion_with_retry
return embeddings.client.create(**kwargs)
File "/usr/local/lib/python3.10/dist-packages/openai/api_resources/embedding.py", line 33, in create
response = super().create(*args, **kwargs)
File "/usr/local/lib/python3.10/dist-packages/openai/api_resources/abstract/engine_api_resource.py", line 153, in create
response, _, api_key = requestor.request(
File "/usr/local/lib/python3.10/dist-packages/openai/api_requestor.py", line 216, in request
result = self.request_raw(
File "/usr/local/lib/python3.10/dist-packages/openai/api_requestor.py", line 516, in request_raw
result = _thread_context.session.request(
File "/usr/local/lib/python3.10/dist-packages/requests/sessions.py", line 587, in request
resp = self.send(prep, **send_kwargs)
File "/usr/local/lib/python3.10/dist-packages/requests/sessions.py", line 701, in send
r = adapter.send(request, **kwargs)
File "/usr/local/lib/python3.10/dist-packages/requests/adapters.py", line 489, in send
resp = conn.urlopen(
File "/usr/local/lib/python3.10/dist-packages/urllib3/connectionpool.py", line 703, in urlopen
httplib_response = self._make_request(
File "/usr/local/lib/python3.10/dist-packages/urllib3/connectionpool.py", line 398, in _make_request
conn.request(method, url, **httplib_request_kw)
File "/usr/local/lib/python3.10/dist-packages/urllib3/connection.py", line 239, in request
super(HTTPConnection, self).request(method, url, body=body, headers=headers)
File "/usr/lib/python3.10/http/client.py", line 1282, in request
self._send_request(method, url, body, headers, encode_chunked)
File "/usr/lib/python3.10/http/client.py", line 1323, in _send_request
self.putheader(hdr, value)
File "/usr/local/lib/python3.10/dist-packages/urllib3/connection.py", line 224, in putheader
_HTTPConnection.putheader(self, header, *values)
File "/usr/lib/python3.10/http/client.py", line 1255, in putheader
values[i] = one_value.encode('latin-1')
UnicodeEncodeError: 'latin-1' codec can't encode character '\u201c' in position 7: ordinal not in range(256)

I've already tried(function ingest_docs() in ingest.py):
loader = ReadTheDocsLoader("langchain.readthedocs.io/en/latest/")
and
loader = ReadTheDocsLoader("langchain.readthedocs.io/en/latest/", encoding="utf-8")
And both show above error.

How to solve the problem? Very appreciated

A bit more insight on the thought process of the prompt chain

Hi @hwchase17,

I'm trying to build a ChatBot and was trying to understand this part of the code. If you can add more color to why this flow, it would be helpful.

def get_new_chain1(vectorstore) -> Chain:
    WEAVIATE_URL = os.environ["WEAVIATE_URL"]
    client = weaviate.Client(
        url=WEAVIATE_URL,
        additional_headers={"X-OpenAI-Api-Key": os.environ["OPENAI_API_KEY"]},
    )

    _eg_template = """## Example:

    Chat History:
    {chat_history}
    Follow Up Input: {question}
    Standalone question: {answer}"""
    _eg_prompt = PromptTemplate(
        template=_eg_template,
        input_variables=["chat_history", "question", "answer"],
    )

    _prefix = """Given the following conversation and a follow up question, rephrase the follow up question to be a standalone question. You should assume that the question is related to LangChain."""
    _suffix = """## Example:

    Chat History:
    {chat_history}
    Follow Up Input: {question}
    Standalone question:"""
    eg_store = Weaviate(
        client,
        "Rephrase",
        "content",
        attributes=["question", "answer", "chat_history"],
    )
    example_selector = SemanticSimilarityExampleSelector(vectorstore=eg_store, k=4)
    prompt = FewShotPromptTemplate(
        prefix=_prefix,
        suffix=_suffix,
        example_selector=example_selector,
        example_prompt=_eg_prompt,
        input_variables=["question", "chat_history"],
    )
    llm = OpenAI(temperature=0, model_name="text-davinci-003")
    key_word_extractor = LLMChain(llm=llm, prompt=prompt)

    EXAMPLE_PROMPT = PromptTemplate(
        template=">Example:\nContent:\n---------\n{page_content}\n----------\nSource: {source}",
        input_variables=["page_content", "source"],
    )
    template = """You are an AI assistant for the open source library LangChain. The documentation is located at https://langchain.readthedocs.io.
You are given the following extracted parts of a long document and a question. Provide a conversational answer with a hyperlink to the documentation.
You should only use hyperlinks that are explicitly listed as a source in the context. Do NOT make up a hyperlink that is not listed.
If the question includes a request for code, provide a code block directly from the documentation.
If you don't know the answer, just say "Hmm, I'm not sure." Don't try to make up an answer.
If the question is not about LangChain, politely inform them that you are tuned to only answer questions about LangChain.
Question: {question}
=========
{context}
=========
Answer in Markdown:"""
    PROMPT = PromptTemplate(template=template, input_variables=["question", "context"])
    doc_chain = load_qa_chain(
        OpenAI(temperature=0, model_name="text-davinci-003", max_tokens=-1),
        chain_type="stuff",
        prompt=PROMPT,
        document_prompt=EXAMPLE_PROMPT,
    )
    return CustomChain(
        chain=doc_chain, vstore=vectorstore, key_word_extractor=key_word_extractor
    )

And I couldn't find much information on document_prompt in the LangChain documentation.

Thank you!

Keeps asking for API key, even once I have entered one.

Screenshot 2023-01-23 at 8 45 59 PM

I create a key to get the app working, but then once its running it asks for one again, I tried using the same one referenced in the OPEN_API_KEY variable, as well as a fresh one, neither seems to get it to return an answer.

Has ReadTheDocsLoader any problems?

Traceback (most recent call last):
File "/root/chat-langchain/ingest.py", line 4, in
from langchain.document_loaders import ReadTheDocsLoader
File "/root/.pyenv/versions/3.9.0b4/lib/python3.9/site-packages/langchain/init.py", line 5, in
from langchain.agents import MRKLChain, ReActChain, SelfAskWithSearchChain
File "/root/.pyenv/versions/3.9.0b4/lib/python3.9/site-packages/langchain/agents/init.py", line 2, in
from langchain.agents.agent import Agent, AgentExecutor
File "/root/.pyenv/versions/3.9.0b4/lib/python3.9/site-packages/langchain/agents/agent.py", line 13, in
from langchain.agents.tools import InvalidTool
File "/root/.pyenv/versions/3.9.0b4/lib/python3.9/site-packages/langchain/agents/tools.py", line 5, in
from langchain.tools.base import BaseTool
File "/root/.pyenv/versions/3.9.0b4/lib/python3.9/site-packages/langchain/tools/init.py", line 3, in
from langchain.tools.base import BaseTool
File "/root/.pyenv/versions/3.9.0b4/lib/python3.9/site-packages/langchain/tools/base.py", line 8, in
from langchain.callbacks import get_callback_manager
File "/root/.pyenv/versions/3.9.0b4/lib/python3.9/site-packages/langchain/callbacks/init.py", line 14, in
from langchain.callbacks.tracers import SharedLangChainTracer
File "/root/.pyenv/versions/3.9.0b4/lib/python3.9/site-packages/langchain/callbacks/tracers/init.py", line 3, in
from langchain.callbacks.tracers.base import SharedTracer, Tracer
File "/root/.pyenv/versions/3.9.0b4/lib/python3.9/site-packages/langchain/callbacks/tracers/base.py", line 12, in
from langchain.callbacks.tracers.schemas import (
File "/root/.pyenv/versions/3.9.0b4/lib/python3.9/site-packages/langchain/callbacks/tracers/schemas.py", line 52, in
class ChainRun(BaseRun):
File "pydantic/main.py", line 286, in pydantic.main.ModelMetaclass.new
File "pydantic/main.py", line 808, in pydantic.main.BaseModel.try_update_forward_refs
File "pydantic/typing.py", line 553, in pydantic.typing.update_model_forward_refs

File "pydantic/typing.py", line 519, in pydantic.typing.update_field_forward_refs
checkers. They serve as the parameters for generic types as well
File "pydantic/typing.py", line 65, in pydantic.typing.evaluate_forwardref
'Sized',
TypeError: _evaluate() takes 3 positional arguments but 4 were given

by the way,I installed the version of langchain is :
langchain 0.0.125

Unable to run the example, Weaviate returns 422 and also unable to run ingest.py class "Paragraph" not found

I tried running python app.py and asking a question in the GUI,

Running on local URL:  http://127.0.0.1:7860

To create a public link, set `share=True` in `launch()`.

==== date/time: 2023-01-28 15:20:38.954878 ====
inp: What is text summary?
What is text summary?
Traceback (most recent call last):
  File "/Users/ziscore/Work/2023/chat-langchain/venv/lib/python3.8/site-packages/gradio/routes.py", line 337, in run_predict
    output = await app.get_blocks().process_api(
  File "/Users/ziscore/Work/2023/chat-langchain/venv/lib/python3.8/site-packages/gradio/blocks.py", line 1015, in process_api
    result = await self.call_function(
  File "/Users/ziscore/Work/2023/chat-langchain/venv/lib/python3.8/site-packages/gradio/blocks.py", line 833, in call_function
    prediction = await anyio.to_thread.run_sync(
  File "/Users/ziscore/Work/2023/chat-langchain/venv/lib/python3.8/site-packages/anyio/to_thread.py", line 31, in run_sync
    return await get_asynclib().run_sync_in_worker_thread(
  File "/Users/ziscore/Work/2023/chat-langchain/venv/lib/python3.8/site-packages/anyio/_backends/_asyncio.py", line 937, in run_sync_in_worker_thread
    return await future
  File "/Users/ziscore/Work/2023/chat-langchain/venv/lib/python3.8/site-packages/anyio/_backends/_asyncio.py", line 867, in run
    result = context.run(func, *args)
  File "app.py", line 39, in chat
    output = agent({"question": inp, "chat_history": history})
  File "/Users/ziscore/Work/2023/chat-langchain/venv/lib/python3.8/site-packages/langchain/chains/base.py", line 146, in __call__
    raise e
  File "/Users/ziscore/Work/2023/chat-langchain/venv/lib/python3.8/site-packages/langchain/chains/base.py", line 142, in __call__
    outputs = self._call(inputs)
  File "/Users/ziscore/Work/2023/chat-langchain/chain.py", line 45, in _call
    docs = self.vstore.similarity_search(new_question, k=4)
  File "/Users/ziscore/Work/2023/chat-langchain/venv/lib/python3.8/site-packages/langchain/vectorstores/weaviate.py", line 78, in similarity_search
    result = query_obj.with_near_text(content).with_limit(k).do()
  File "/Users/ziscore/Work/2023/chat-langchain/venv/lib/python3.8/site-packages/weaviate/gql/filter.py", line 73, in do
    raise UnexpectedStatusCodeException("Query was not successful", response)
weaviate.exceptions.UnexpectedStatusCodeException: Query was not successful! Unexpected status code: 422, with response body: {'error': [{'message': 'no graphql provider present, this is most likely because no schema is present. Import a schema first!'}]}.
^CKeyboard interruption in main thread... closing server.
^CKeyboard interruption in main thread... closing server.

I guess this is because the weaviate URL doesn't have a schema. So I ran,

wget -r -A.html https://langchain.readthedocs.io/en/latest/
python3 ingest.py

However this returns another error that class "Paragraph" not found.

[...]
Created a chunk of size 1755, which is longer than the specified 1000
Traceback (most recent call last):
  File "ingest.py", line 42, in <module>
    client.schema.delete_class("Paragraph")
  File "/Users/ziscore/Work/2023/chat-langchain/venv/lib/python3.8/site-packages/weaviate/schema/crud_schema.py", line 201, in delete_class
    raise UnexpectedStatusCodeException("Delete class from schema", response)
weaviate.exceptions.UnexpectedStatusCodeException: Delete class from schema! Unexpected status code: 400, with response body: {'error': [{'message': "could not find class 'Paragraph'"}]}.

I'm running Python 3.8.13 on MacOS if that makes any difference

Can't display LangChain streaming response in Streamlit (python app framework)

I'm trying to display the streaming output from ChatGPT api to Streamlit (a python web app framework). The goal is to make it feel like the bot is typing back to us. However, langchain.chat_models.ChatOpenAI does not output a generator that we can use in Streamlit. Is there anyway I can make this effect in Streamlit using LangChain?

This is what I tried with LangChain. The command llm_ChatOpenAI(messages) in the code snippet below does streaming effect in the background and python console, but not in Streamlit UI.

import streamlit as st
from langchain.chat_models import ChatOpenAI
from langchain.callbacks.base import CallbackManager
from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler
from langchain.schema import HumanMessage

OPENAI_API_KEY = 'XXX'
model_name = "gpt-4-0314"
user_text = "Tell me about Seattle in 10 words."

llm_ChatOpenAI = ChatOpenAI(
    streaming=True, 
    verbose=True,
    temperature=0.0,
    model=model_name,
    openai_api_key=OPENAI_API_KEY,
    callback_manager=CallbackManager([StreamingStdOutCallbackHandler()]), 
    )

messages = [
    HumanMessage(content=user_text)
]

# [Doesn't work] Looping over the response for "streaming effect"
for resp in llm_ChatOpenAI(messages):
    st.write(resp) 

Since the code above does not work, I have to use the plain vanilla openai, which is a generator, without LangChain wrapper. The code below will display streaming effect with Streamlit:

import openai
openai.api_key = OPENAI_API_KEY
llm_direct = openai.ChatCompletion.create(
            model=model_name, 
            messages=[{"role": "user", "content": user_text}],
            temperature=0.0,
            max_tokens=50,
            stream = True,
        )

tokens = []
# Works well -- Looping over the response for "streaming effect"
for resp in llm_direct:
    if resp.get("choices") and resp["choices"][0].get("delta") and resp["choices"][0]["delta"].get("content"):
        tokens.append( resp["choices"][0]["delta"]["content"] )
        result = "".join(tokens)
        st.write(result) 

I attached the streamlit code that you can run and experiment below:

  • save the file below as main.py
  • install dependencies pip install openai streamlit
  • run the code using streamlit run main.py
# main.py

OPENAI_API_KEY = 'YOUR API KEY'
user_text = "Tell me about Seattle in 10 words."

def render():
    import streamlit as st
    st.set_page_config(layout="wide")
    st.write("Welcome to GPT Applications!")

    model_name = st.radio("Choose a model", ["text-davinci-003", "gpt-4-0314", "gpt-3.5-turbo"])
    api_choice = st.radio("Choose an API", ["openai.ChatCompletion", "langchain.llms.OpenAI", "langchain.chat_models.ChatOpenAI"])
    res_box = st.empty()
    
    if st.button("Run", type='primary'):
        if api_choice == "openai.ChatCompletion":
            import openai
            openai.api_key = OPENAI_API_KEY
            llm_direct = openai.ChatCompletion.create(
                        model=model_name, 
                        messages=[{"role": "user", "content": user_text}],
                        temperature=0.0,
                        max_tokens=50,
                        stream = True,
                    )
            
            tokens = []
            # Works well -- Looping over the response for "streaming effect"
            for resp in llm_direct:
                if resp.get("choices") and resp["choices"][0].get("delta") and resp["choices"][0]["delta"].get("content"):
                    tokens.append( resp["choices"][0]["delta"]["content"] )
                    result = "".join(tokens)
                    res_box.write(result) 


        elif api_choice == "langchain.llms.OpenAI":
            from langchain.llms import OpenAI

            llm_OpenAI = OpenAI(
                streaming=True, 
                verbose=True, 
                temperature=0.0,
                model_name=model_name,
                openai_api_key=OPENAI_API_KEY
            )

            response = llm_OpenAI.stream(user_text)  # returns a generator. However, it doesn't work with model_name='gpt-4'
            tokens = []
            for resp in response:
                tokens.append(resp["choices"][0]["text"])
                result = "".join(tokens)
                res_box.write(result)


        elif api_choice == "langchain.chat_models.ChatOpenAI":
            from langchain.chat_models import ChatOpenAI
            from langchain.callbacks.base import CallbackManager
            from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler
            from langchain.schema import HumanMessage

            llm_ChatOpenAI = ChatOpenAI(
                streaming=True, 
                verbose=True,
                temperature=0.0,
                model=model_name,
                openai_api_key=OPENAI_API_KEY,
                callback_manager=CallbackManager([StreamingStdOutCallbackHandler()]), 
                )

            messages = [
                HumanMessage(content=user_text)
            ]

            # [Doesn't work] Looping over the response for "streaming effect"
            for resp in llm_ChatOpenAI(messages):
                res_box.write(resp)


if __name__ == '__main__':
    render()

Is it possible to create an index and have the [chroma] vectorstore persisted?

Hi,

It seems to me that I cant persist the vectorstore if I want to create an index?

a) The only way to create an index is to instantiate it together with the vectorstore, with a loader

index = VectorstoreIndexCreator().from_loaders([loader])

ie the index can only be created at the same time as the vectorstore is initialized

b) The only way to support persistence in the vectorstore is also on initialization

vectordb = Chroma.from_documents(documents=docs, embedding=embedding, persist_directory=persist_directory)

So these two initializations of the vector store can not both happen, therefore I cant create a persistent vectorstore with index, or did I miss something?

Stuck loading

Hi. I managed to run this in my local machine, but when I asked a question, what should be parameter for llm, it got stuck.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.