2023-10-31 15:49:24 +00:00
|
|
|
import os
|
|
|
|
|
2024-01-02 20:32:16 +00:00
|
|
|
from langchain_community.embeddings import VertexAIEmbeddings
|
|
|
|
from langchain_community.llms import VertexAI
|
2024-01-02 21:47:11 +00:00
|
|
|
from langchain_community.vectorstores import MatchingEngine
|
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-01-03 21:28:05 +00:00
|
|
|
from langchain_core.prompts import PromptTemplate
|
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.pydantic_v1 import BaseModel
|
|
|
|
from langchain_core.runnables import RunnableParallel, RunnablePassthrough
|
2023-10-31 15:49:24 +00:00
|
|
|
|
|
|
|
# you need to preate the index first, for example, as described here:
|
|
|
|
# https://github.com/GoogleCloudPlatform/generative-ai/blob/main/language/use-cases/document-qa/question_answering_documents_langchain_matching_engine.ipynb
|
|
|
|
expected_variables = [
|
|
|
|
"project_id",
|
|
|
|
"me_region",
|
|
|
|
"gcs_bucket",
|
|
|
|
"me_index_id",
|
|
|
|
"me_endpoint_id",
|
|
|
|
]
|
|
|
|
variables = []
|
|
|
|
for variable_name in expected_variables:
|
|
|
|
variable = os.environ.get(variable_name.upper())
|
|
|
|
if not variable:
|
|
|
|
raise Exception(f"Missing `{variable_name}` environment variable.")
|
|
|
|
variables.append(variable)
|
|
|
|
|
|
|
|
project_id, me_region, gcs_bucket, me_index_id, me_endpoint_id = variables
|
|
|
|
|
|
|
|
|
|
|
|
vectorstore = MatchingEngine.from_components(
|
|
|
|
project_id=project_id,
|
|
|
|
region=me_region,
|
|
|
|
gcs_bucket_name=gcs_bucket,
|
|
|
|
embedding=VertexAIEmbeddings(),
|
|
|
|
index_id=me_index_id,
|
|
|
|
endpoint_id=me_endpoint_id,
|
|
|
|
)
|
|
|
|
|
|
|
|
model = VertexAI()
|
|
|
|
|
|
|
|
template = (
|
|
|
|
"SYSTEM: You are an intelligent assistant helping the users with their questions"
|
|
|
|
"on research papers.\n\n"
|
|
|
|
"Question: {question}\n\n"
|
|
|
|
"Strictly Use ONLY the following pieces of context to answer the question at the "
|
|
|
|
"end. Think step-by-step and then answer.\n\n"
|
|
|
|
"Do not try to make up an answer:\n"
|
|
|
|
"- If the answer to the question cannot be determined from the context alone, "
|
|
|
|
'say \n"I cannot determine the answer to that."\n'
|
|
|
|
'- If the context is empty, just say "I do not know the answer to that."\n\n'
|
|
|
|
"=============\n{context}\n=============\n\n"
|
|
|
|
"Question: {question}\nHelpful Answer: "
|
|
|
|
)
|
|
|
|
|
|
|
|
prompt = PromptTemplate.from_template(template)
|
|
|
|
|
|
|
|
retriever = vectorstore.as_retriever(
|
|
|
|
search_type="similarity",
|
|
|
|
search_kwargs={
|
|
|
|
"k": 10,
|
|
|
|
"search_distance": 0.6,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
chain = (
|
|
|
|
RunnableParallel({"context": retriever, "question": RunnablePassthrough()})
|
|
|
|
| prompt
|
|
|
|
| model
|
|
|
|
| StrOutputParser()
|
|
|
|
)
|
2023-11-01 00:13:44 +00:00
|
|
|
|
|
|
|
|
|
|
|
# Add typing for input
|
|
|
|
class Question(BaseModel):
|
|
|
|
__root__: str
|
|
|
|
|
|
|
|
|
|
|
|
chain = chain.with_types(input_type=Question)
|