Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Serialize all the chains! #761

Merged
merged 5 commits into from
Jan 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions langchain/chains/api/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from typing import Any, Dict, List, Optional

from pydantic import BaseModel, root_validator
from pydantic import BaseModel, Field, root_validator

from langchain.chains.api.prompt import API_RESPONSE_PROMPT, API_URL_PROMPT
from langchain.chains.base import Chain
Expand All @@ -18,7 +18,7 @@ class APIChain(Chain, BaseModel):

api_request_chain: LLMChain
api_answer_chain: LLMChain
requests_wrapper: RequestsWrapper
requests_wrapper: RequestsWrapper = Field(exclude=True)
api_docs: str
question_key: str = "question" #: :meta private:
output_key: str = "output" #: :meta private:
Expand Down Expand Up @@ -102,3 +102,7 @@ def from_llm_and_api_docs(
api_docs=api_docs,
**kwargs,
)

@property
def _chain_type(self) -> str:
return "api_chain"
4 changes: 4 additions & 0 deletions langchain/chains/combine_documents/map_reduce.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,3 +168,7 @@ def combine_docs(
extra_return_dict = {}
output, _ = self.combine_document_chain.combine_docs(result_docs, **kwargs)
return output, extra_return_dict

@property
def _chain_type(self) -> str:
return "map_reduce_documents_chain"
4 changes: 4 additions & 0 deletions langchain/chains/combine_documents/map_rerank.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,7 @@ def combine_docs(self, docs: List[Document], **kwargs: Any) -> Tuple[str, dict]:
if self.return_intermediate_steps:
extra_info["intermediate_steps"] = results
return output[self.answer_key], extra_info

@property
def _chain_type(self) -> str:
return "map_rerank_documents_chain"
4 changes: 4 additions & 0 deletions langchain/chains/combine_documents/refine.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,3 +113,7 @@ def combine_docs(self, docs: List[Document], **kwargs: Any) -> Tuple[str, dict]:
else:
extra_return_dict = {}
return res, extra_return_dict

@property
def _chain_type(self) -> str:
return "refine_documents_chain"
4 changes: 4 additions & 0 deletions langchain/chains/combine_documents/stuff.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,7 @@ def combine_docs(self, docs: List[Document], **kwargs: Any) -> Tuple[str, dict]:
inputs = self._get_inputs(docs, **kwargs)
# Call predict on the LLM.
return self.llm_chain.predict(**inputs), {}

@property
def _chain_type(self) -> str:
return "stuff_documents_chain"
4 changes: 4 additions & 0 deletions langchain/chains/hyde/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,7 @@ def from_llm(
prompt = PROMPT_MAP[prompt_key]
llm_chain = LLMChain(llm=llm, prompt=prompt)
return cls(base_embeddings=base_embeddings, llm_chain=llm_chain)

@property
def _chain_type(self) -> str:
return "hyde_chain"
4 changes: 4 additions & 0 deletions langchain/chains/llm_bash/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,7 @@ def _call(self, inputs: Dict[str, str]) -> Dict[str, str]:
else:
raise ValueError(f"unknown format from LLM: {t}")
return {self.output_key: output}

@property
def _chain_type(self) -> str:
return "llm_bash_chain"
4 changes: 4 additions & 0 deletions langchain/chains/llm_checker/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,7 @@ def _call(self, inputs: Dict[str, str]) -> Dict[str, str]:
)
output = question_to_checked_assertions_chain({"question": question})
return {self.output_key: output["revised_statement"]}

@property
def _chain_type(self) -> str:
return "llm_checker_chain"
4 changes: 4 additions & 0 deletions langchain/chains/llm_math/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,7 @@ def _call(self, inputs: Dict[str, str]) -> Dict[str, str]:
else:
raise ValueError(f"unknown format from LLM: {t}")
return {self.output_key: answer}

@property
def _chain_type(self) -> str:
return "llm_math_chain"
8 changes: 7 additions & 1 deletion langchain/chains/llm_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ class LLMRequestsChain(Chain, BaseModel):
"""Chain that hits a URL and then uses an LLM to parse results."""

llm_chain: LLMChain
requests_wrapper: RequestsWrapper = Field(default_factory=RequestsWrapper)
requests_wrapper: RequestsWrapper = Field(
default_factory=RequestsWrapper, exclude=True
)
text_length: int = 8000
requests_key: str = "requests_result" #: :meta private:
input_key: str = "url" #: :meta private:
Expand Down Expand Up @@ -71,3 +73,7 @@ def _call(self, inputs: Dict[str, str]) -> Dict[str, str]:
other_keys[self.requests_key] = soup.get_text()[: self.text_length]
result = self.llm_chain.predict(**other_keys)
return {self.output_key: result}

@property
def _chain_type(self) -> str:
return "llm_requests_chain"
Loading