-
I'm creating a service, besides the content and prompt, that allows input a from langchain.output_parsers import PydanticOutputParser
from langchain_core.output_parsers import JsonOutputParser
class OneContentToOneRequest(CustomUserType):
content: str
prompt: str
output_format_sample_json_str: str = None
model = ChatOpenAI()
def create_prompt(request: OneContentToOneRequest):
from pydantic import BaseModel, create_model
import json
output_format_sample_json = json.loads(
request.output_format_sample_json_str)
output_format_pydantic_model = create_model(
'output_format_pydantic_model', **output_format_sample_json)
output_format_pydantic_model = output_format_pydantic_model
# Set up a parser + inject instructions into the prompt template.
parser = PydanticOutputParser(
pydantic_object=output_format_pydantic_model)
return {"system_prompt": request.prompt, "format_instructions": parser.get_format_instructions(), "user_content": request.content}
prompt = ChatPromptTemplate.from_messages([
("system", '{system_prompt}\n{format_instructions}'),
("human", '{user_content}')
])
add_routes(
app,
RunnableLambda(create_prompt).with_types(
input_type=OneContentToOneRequest) | prompt | model | how_to_write_this_dynamic_parser ,
path="/MyService1",
)
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=9000) this code obvious can't work as the last part of chain: |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 4 replies
-
is If so, you can instantiate the entire chain inside the runnable lambda and call def runnable_lambda(request):
some stuff
chain = prompt | model | parser
response = chain.invoke(...)
return response |
Beta Was this translation helpful? Give feedback.
-
You can do something like this for streaming support. Things might not be rendered nicely in the playground (not sure). Alternatively, you can use I don't believe that this will stream well because it uses a pydantic parser which doesn't operate on input stream. See details here: https://python.langchain.com/docs/expression_language/streaming. Your options are to do something with Another option is to try to use JSONParser and then follow up with a custom parser that uses the pydantic model to parse the json once its complete. import json
from fastapi import FastAPI
from langchain.output_parsers import PydanticOutputParser
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.runnables import RunnableLambda
from langchain_openai import ChatOpenAI
from pydantic.v1 import create_model
from langserve import CustomUserType, add_routes
class OneContentToOneRequest(CustomUserType):
content: str
prompt: str
output_format_sample_json_str: str = None
model = ChatOpenAI()
async def run_extraction(request: OneContentToOneRequest):
"""Run an extraction on the given content.
This function uses an async generator so that streaming is supposed.
"""
output_format_sample_json = json.loads(
request.output_format_sample_json_str)
output_format_pydantic_model = create_model(
'output_format_pydantic_model', **output_format_sample_json)
output_format_pydantic_model = output_format_pydantic_model
# Set up a parser + inject instructions into the prompt template.
parser = PydanticOutputParser(
pydantic_object=output_format_pydantic_model)
input = {"system_prompt": request.prompt,
"format_instructions": parser.get_format_instructions(),
"user_content": request.content}
chain = prompt | model | parser
async for chunk in chain.astream(input):
yield chunk
prompt = ChatPromptTemplate.from_messages([
("system", '{system_prompt}\n{format_instructions}'),
("human", '{user_content}')
])
app = FastAPI()
add_routes(
app,
RunnableLambda(run_extraction),
path="/MyService1",
)
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=9000) |
Beta Was this translation helpful? Give feedback.
You can do something like this for streaming support. Things might not be rendered nicely in the playground (not sure).
Alternatively, you can use
APIHandler
directly (see those examples) to create a runnable and then stream from it.I don't believe that this will stream well because it uses a pydantic parser which doesn't operate on input stream. See details here: https://python.langchain.com/docs/expression_language/streaming.
Your options are to do something with
stream_events
endpoint instead which will yield chunks from the model.Another option is to try to use JSONParser and then follow up with a custom parser that uses the pydantic model to parse the json once its complete.