2023-10-29 04:48:17 +00:00
|
|
|
import base64
|
|
|
|
import json
|
|
|
|
|
|
|
|
from langchain.chat_models import ChatOpenAI
|
|
|
|
from langchain.prompts import ChatPromptTemplate, SystemMessagePromptTemplate
|
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
|
|
|
|
from langchain_core.pydantic_v1 import Field
|
2023-10-29 04:48:17 +00:00
|
|
|
from langserve import CustomUserType
|
|
|
|
|
|
|
|
from .prompts import (
|
|
|
|
AI_REPONSE_DICT,
|
|
|
|
FULL_PROMPT,
|
|
|
|
USER_EXAMPLE_DICT,
|
|
|
|
create_prompt,
|
|
|
|
)
|
|
|
|
from .utils import parse_llm_output
|
|
|
|
|
|
|
|
llm = ChatOpenAI(temperature=0, model="gpt-4")
|
|
|
|
|
|
|
|
prompt = ChatPromptTemplate.from_messages(
|
|
|
|
[
|
|
|
|
SystemMessagePromptTemplate.from_template(FULL_PROMPT),
|
|
|
|
("human", "{user_example}"),
|
|
|
|
("ai", "{ai_response}"),
|
|
|
|
("human", "{input}"),
|
|
|
|
],
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
# ATTENTION: Inherit from CustomUserType instead of BaseModel otherwise
|
|
|
|
# the server will decode it into a dict instead of a pydantic model.
|
|
|
|
class FileProcessingRequest(CustomUserType):
|
|
|
|
"""Request including a base64 encoded file."""
|
|
|
|
|
|
|
|
# The extra field is used to specify a widget for the playground UI.
|
|
|
|
file: str = Field(..., extra={"widget": {"type": "base64file"}})
|
|
|
|
num_plates: int = None
|
|
|
|
num_rows: int = 8
|
|
|
|
num_cols: int = 12
|
|
|
|
|
|
|
|
|
|
|
|
def _load_file(request: FileProcessingRequest):
|
2023-10-29 22:50:09 +00:00
|
|
|
return base64.b64decode(request.file.encode("utf-8")).decode("utf-8")
|
2023-10-29 04:48:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
def _load_prompt(request: FileProcessingRequest):
|
|
|
|
return create_prompt(
|
2023-10-29 22:50:09 +00:00
|
|
|
num_plates=request.num_plates,
|
|
|
|
num_rows=request.num_rows,
|
|
|
|
num_cols=request.num_cols,
|
|
|
|
)
|
|
|
|
|
2023-10-29 04:48:17 +00:00
|
|
|
|
|
|
|
def _get_col_range_str(request: FileProcessingRequest):
|
|
|
|
if request.num_cols:
|
|
|
|
return f"from 1 to {request.num_cols}"
|
|
|
|
else:
|
|
|
|
return ""
|
|
|
|
|
|
|
|
|
|
|
|
def _get_json_format(request: FileProcessingRequest):
|
|
|
|
return json.dumps(
|
|
|
|
[
|
|
|
|
{
|
|
|
|
"row_start": 12,
|
|
|
|
"row_end": 12 + request.num_rows - 1,
|
|
|
|
"col_start": 1,
|
|
|
|
"col_end": 1 + request.num_cols - 1,
|
|
|
|
"contents": "Entity ID",
|
|
|
|
}
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
2023-10-29 22:50:09 +00:00
|
|
|
|
2023-10-29 04:48:17 +00:00
|
|
|
chain = (
|
|
|
|
{
|
|
|
|
# Should add validation to ensure numeric indices
|
|
|
|
"input": _load_file,
|
|
|
|
"hint": _load_prompt,
|
|
|
|
"col_range_str": _get_col_range_str,
|
|
|
|
"json_format": _get_json_format,
|
2023-10-29 22:50:09 +00:00
|
|
|
"user_example": lambda x: USER_EXAMPLE_DICT[x.num_rows * x.num_cols],
|
|
|
|
"ai_response": lambda x: AI_REPONSE_DICT[x.num_rows * x.num_cols],
|
2023-10-29 04:48:17 +00:00
|
|
|
}
|
|
|
|
| prompt
|
|
|
|
| llm
|
|
|
|
| StrOutputParser()
|
|
|
|
| parse_llm_output
|
|
|
|
).with_types(input_type=FileProcessingRequest)
|