2023-11-13 19:44:19 +00:00
|
|
|
import os
|
|
|
|
from operator import itemgetter
|
|
|
|
from typing import List, Tuple
|
|
|
|
|
|
|
|
from langchain.retrievers import SelfQueryRetriever
|
2024-01-02 20:32:16 +00:00
|
|
|
from langchain_community.chat_models import ChatOpenAI
|
|
|
|
from langchain_community.embeddings import OpenAIEmbeddings
|
docs[patch], templates[patch]: Import from core (#14575)
Update imports to use core for the low-hanging fruit changes. Ran
following
```bash
git grep -l 'langchain.schema.runnable' {docs,templates,cookbook} | xargs sed -i '' 's/langchain\.schema\.runnable/langchain_core.runnables/g'
git grep -l 'langchain.schema.output_parser' {docs,templates,cookbook} | xargs sed -i '' 's/langchain\.schema\.output_parser/langchain_core.output_parsers/g'
git grep -l 'langchain.schema.messages' {docs,templates,cookbook} | xargs sed -i '' 's/langchain\.schema\.messages/langchain_core.messages/g'
git grep -l 'langchain.schema.chat_histry' {docs,templates,cookbook} | xargs sed -i '' 's/langchain\.schema\.chat_history/langchain_core.chat_history/g'
git grep -l 'langchain.schema.prompt_template' {docs,templates,cookbook} | xargs sed -i '' 's/langchain\.schema\.prompt_template/langchain_core.prompts/g'
git grep -l 'from langchain.pydantic_v1' {docs,templates,cookbook} | xargs sed -i '' 's/from langchain\.pydantic_v1/from langchain_core.pydantic_v1/g'
git grep -l 'from langchain.tools.base' {docs,templates,cookbook} | xargs sed -i '' 's/from langchain\.tools\.base/from langchain_core.tools/g'
git grep -l 'from langchain.chat_models.base' {docs,templates,cookbook} | xargs sed -i '' 's/from langchain\.chat_models.base/from langchain_core.language_models.chat_models/g'
git grep -l 'from langchain.llms.base' {docs,templates,cookbook} | xargs sed -i '' 's/from langchain\.llms\.base\ /from langchain_core.language_models.llms\ /g'
git grep -l 'from langchain.embeddings.base' {docs,templates,cookbook} | xargs sed -i '' 's/from langchain\.embeddings\.base/from langchain_core.embeddings/g'
git grep -l 'from langchain.vectorstores.base' {docs,templates,cookbook} | xargs sed -i '' 's/from langchain\.vectorstores\.base/from langchain_core.vectorstores/g'
git grep -l 'from langchain.agents.tools' {docs,templates,cookbook} | xargs sed -i '' 's/from langchain\.agents\.tools/from langchain_core.tools/g'
git grep -l 'from langchain.schema.output' {docs,templates,cookbook} | xargs sed -i '' 's/from langchain\.schema\.output\ /from langchain_core.outputs\ /g'
git grep -l 'from langchain.schema.embeddings' {docs,templates,cookbook} | xargs sed -i '' 's/from langchain\.schema\.embeddings/from langchain_core.embeddings/g'
git grep -l 'from langchain.schema.document' {docs,templates,cookbook} | xargs sed -i '' 's/from langchain\.schema\.document/from langchain_core.documents/g'
git grep -l 'from langchain.schema.agent' {docs,templates,cookbook} | xargs sed -i '' 's/from langchain\.schema\.agent/from langchain_core.agents/g'
git grep -l 'from langchain.schema.prompt ' {docs,templates,cookbook} | xargs sed -i '' 's/from langchain\.schema\.prompt\ /from langchain_core.prompt_values /g'
git grep -l 'from langchain.schema.language_model' {docs,templates,cookbook} | xargs sed -i '' 's/from langchain\.schema\.language_model/from langchain_core.language_models/g'
```
2023-12-12 00:49:10 +00:00
|
|
|
from langchain_core.output_parsers import StrOutputParser
|
2024-02-22 23:58:44 +00:00
|
|
|
from langchain_core.prompts import format_document
|
2023-12-12 23:31:14 +00:00
|
|
|
from langchain_core.pydantic_v1 import BaseModel, Field
|
docs[patch], templates[patch]: Import from core (#14575)
Update imports to use core for the low-hanging fruit changes. Ran
following
```bash
git grep -l 'langchain.schema.runnable' {docs,templates,cookbook} | xargs sed -i '' 's/langchain\.schema\.runnable/langchain_core.runnables/g'
git grep -l 'langchain.schema.output_parser' {docs,templates,cookbook} | xargs sed -i '' 's/langchain\.schema\.output_parser/langchain_core.output_parsers/g'
git grep -l 'langchain.schema.messages' {docs,templates,cookbook} | xargs sed -i '' 's/langchain\.schema\.messages/langchain_core.messages/g'
git grep -l 'langchain.schema.chat_histry' {docs,templates,cookbook} | xargs sed -i '' 's/langchain\.schema\.chat_history/langchain_core.chat_history/g'
git grep -l 'langchain.schema.prompt_template' {docs,templates,cookbook} | xargs sed -i '' 's/langchain\.schema\.prompt_template/langchain_core.prompts/g'
git grep -l 'from langchain.pydantic_v1' {docs,templates,cookbook} | xargs sed -i '' 's/from langchain\.pydantic_v1/from langchain_core.pydantic_v1/g'
git grep -l 'from langchain.tools.base' {docs,templates,cookbook} | xargs sed -i '' 's/from langchain\.tools\.base/from langchain_core.tools/g'
git grep -l 'from langchain.chat_models.base' {docs,templates,cookbook} | xargs sed -i '' 's/from langchain\.chat_models.base/from langchain_core.language_models.chat_models/g'
git grep -l 'from langchain.llms.base' {docs,templates,cookbook} | xargs sed -i '' 's/from langchain\.llms\.base\ /from langchain_core.language_models.llms\ /g'
git grep -l 'from langchain.embeddings.base' {docs,templates,cookbook} | xargs sed -i '' 's/from langchain\.embeddings\.base/from langchain_core.embeddings/g'
git grep -l 'from langchain.vectorstores.base' {docs,templates,cookbook} | xargs sed -i '' 's/from langchain\.vectorstores\.base/from langchain_core.vectorstores/g'
git grep -l 'from langchain.agents.tools' {docs,templates,cookbook} | xargs sed -i '' 's/from langchain\.agents\.tools/from langchain_core.tools/g'
git grep -l 'from langchain.schema.output' {docs,templates,cookbook} | xargs sed -i '' 's/from langchain\.schema\.output\ /from langchain_core.outputs\ /g'
git grep -l 'from langchain.schema.embeddings' {docs,templates,cookbook} | xargs sed -i '' 's/from langchain\.schema\.embeddings/from langchain_core.embeddings/g'
git grep -l 'from langchain.schema.document' {docs,templates,cookbook} | xargs sed -i '' 's/from langchain\.schema\.document/from langchain_core.documents/g'
git grep -l 'from langchain.schema.agent' {docs,templates,cookbook} | xargs sed -i '' 's/from langchain\.schema\.agent/from langchain_core.agents/g'
git grep -l 'from langchain.schema.prompt ' {docs,templates,cookbook} | xargs sed -i '' 's/from langchain\.schema\.prompt\ /from langchain_core.prompt_values /g'
git grep -l 'from langchain.schema.language_model' {docs,templates,cookbook} | xargs sed -i '' 's/from langchain\.schema\.language_model/from langchain_core.language_models/g'
```
2023-12-12 00:49:10 +00:00
|
|
|
from langchain_core.runnables import RunnableParallel, RunnablePassthrough
|
2024-03-06 23:09:12 +00:00
|
|
|
from langchain_elasticsearch.vectorstores import ElasticsearchStore
|
2023-11-13 19:44:19 +00:00
|
|
|
|
|
|
|
from .prompts import CONDENSE_QUESTION_PROMPT, DOCUMENT_PROMPT, LLM_CONTEXT_PROMPT
|
|
|
|
|
|
|
|
ELASTIC_CLOUD_ID = os.getenv("ELASTIC_CLOUD_ID")
|
|
|
|
ELASTIC_USERNAME = os.getenv("ELASTIC_USERNAME", "elastic")
|
|
|
|
ELASTIC_PASSWORD = os.getenv("ELASTIC_PASSWORD")
|
|
|
|
ES_URL = os.getenv("ES_URL", "http://localhost:9200")
|
|
|
|
ELASTIC_INDEX_NAME = os.getenv("ELASTIC_INDEX_NAME", "workspace-search-example")
|
|
|
|
|
|
|
|
if ELASTIC_CLOUD_ID and ELASTIC_USERNAME and ELASTIC_PASSWORD:
|
|
|
|
es_connection_details = {
|
|
|
|
"es_cloud_id": ELASTIC_CLOUD_ID,
|
|
|
|
"es_user": ELASTIC_USERNAME,
|
|
|
|
"es_password": ELASTIC_PASSWORD,
|
|
|
|
}
|
|
|
|
else:
|
|
|
|
es_connection_details = {"es_url": ES_URL}
|
|
|
|
|
|
|
|
vecstore = ElasticsearchStore(
|
|
|
|
ELASTIC_INDEX_NAME,
|
|
|
|
embedding=OpenAIEmbeddings(),
|
|
|
|
**es_connection_details,
|
|
|
|
)
|
|
|
|
|
|
|
|
document_contents = "The purpose and specifications of a workplace policy."
|
|
|
|
metadata_field_info = [
|
|
|
|
{"name": "name", "type": "string", "description": "Name of the workplace policy."},
|
|
|
|
{
|
|
|
|
"name": "created_on",
|
|
|
|
"type": "date",
|
|
|
|
"description": "The date the policy was created in ISO 8601 date format (YYYY-MM-DD).", # noqa: E501
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "updated_at",
|
|
|
|
"type": "date",
|
|
|
|
"description": "The date the policy was last updated in ISO 8601 date format (YYYY-MM-DD).", # noqa: E501
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "location",
|
|
|
|
"type": "string",
|
|
|
|
"description": "Where the policy text is stored. The only valid values are ['github', 'sharepoint'].", # noqa: E501
|
|
|
|
},
|
|
|
|
]
|
|
|
|
llm = ChatOpenAI(temperature=0)
|
|
|
|
retriever = SelfQueryRetriever.from_llm(
|
|
|
|
llm, vecstore, document_contents, metadata_field_info
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def _combine_documents(docs: List) -> str:
|
|
|
|
return "\n\n".join(format_document(doc, prompt=DOCUMENT_PROMPT) for doc in docs)
|
|
|
|
|
|
|
|
|
|
|
|
def _format_chat_history(chat_history: List[Tuple]) -> str:
|
|
|
|
return "\n".join(f"Human: {human}\nAssistant: {ai}" for human, ai in chat_history)
|
|
|
|
|
|
|
|
|
|
|
|
class InputType(BaseModel):
|
|
|
|
question: str
|
|
|
|
chat_history: List[Tuple[str, str]] = Field(default_factory=list)
|
|
|
|
|
|
|
|
|
|
|
|
standalone_question = (
|
|
|
|
{
|
|
|
|
"question": itemgetter("question"),
|
|
|
|
"chat_history": lambda x: _format_chat_history(x["chat_history"]),
|
|
|
|
}
|
|
|
|
| CONDENSE_QUESTION_PROMPT
|
|
|
|
| llm
|
|
|
|
| StrOutputParser()
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def route_question(input):
|
|
|
|
if input.get("chat_history"):
|
|
|
|
return standalone_question
|
|
|
|
else:
|
|
|
|
return RunnablePassthrough()
|
|
|
|
|
|
|
|
|
|
|
|
_context = RunnableParallel(
|
|
|
|
context=retriever | _combine_documents,
|
|
|
|
question=RunnablePassthrough(),
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
chain = (
|
|
|
|
standalone_question | _context | LLM_CONTEXT_PROMPT | llm | StrOutputParser()
|
|
|
|
).with_types(input_type=InputType)
|