How to take in JSON string as user input using ChatPromptTemplate and MessagesPlaceholder #26845
Replies: 1 comment
-
I found a similar unsolved discussion that might be relevant to your issue: LangGraph - Plan-and-Execute Error - Need Help [1]. To take in a JSON string as user input using
Here is the code example: import base64
import json
from langchain_community.chat_models import ChatOpenAI
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder, SystemMessagePromptTemplate
from langchain_core.pydantic_v1 import Field
from langserve import CustomUserType
from langchain.agents import AgentExecutor, create_json_chat_agent
from langchain import hub
# Define the system prompt
system = '''Assistant is a large language model trained by OpenAI.
Assistant is designed to be able to assist with a wide range of tasks, from answering \
simple questions to providing in-depth explanations and discussions on a wide range of \
topics. As a language model, Assistant is able to generate human-like text based on \
the input it receives, allowing it to engage in natural-sounding conversations and \
provide responses that are coherent and relevant to the topic at hand.
Assistant is constantly learning and improving, and its capabilities are constantly \
evolving. It is able to process and understand large amounts of text, and can use this \
knowledge to provide accurate and informative responses to a wide range of questions. \
Additionally, Assistant is able to generate its own text based on the input it \
receives, allowing it to engage in discussions and provide explanations and \
descriptions on a wide range of topics.
Overall, Assistant is a powerful system that can help with a wide range of tasks \
and provide valuable insights and information on a wide range of topics. Whether \
you need help with a specific question or just want to have a conversation about \
a particular topic, Assistant is here to assist.'''
# Define the human prompt
human = '''TOOLS
------
Assistant can ask the user to use tools to look up information that may be helpful in \
answering the users original question. The tools the human can use are:
{tools}
RESPONSE FORMAT INSTRUCTIONS
----------------------------
When responding to me, please output a response in one of two formats:
**Option 1:**
Use this if you want the human to use a tool.
Markdown code snippet formatted in the following schema:
```json
{{
"action": string, \\ The action to take. Must be one of {tool_names}
"action_input": string \\ The input to the action
}} Option #2: {{
"action": "Final Answer",
"action_input": string \\ You should put what you want to return to use here
}} USER'S INPUTHere is the user's input (remember to respond with a markdown code snippet of a json {input}''' Create the ChatPromptTemplateprompt = ChatPromptTemplate.from_messages( Pull the prompt from the hubprompt = hub.pull("hwchase17/react-chat-json") Initialize the model and toolsmodel = ChatOpenAI() Create the JSON chat agentagent = create_json_chat_agent(model, tools, prompt) Create the agent executoragent_executor = AgentExecutor(agent=agent, tools=tools) Invoke the agent with user inputuser_input = '{"input": "hi"}'
|
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Checked other resources
Commit to Help
Example Code
Description
I'm trying to translate JSON string to English using ChatPromptTemplate and MessagesPlaceholder, for instance:
I tried 2 template_format, but encountered error with both approaches:
Can someone enlighten me how to solve the problem? Thanks in advance
System Info
System Information
Package Information
Optional packages not installed
Other Dependencies
Beta Was this translation helpful? Give feedback.
All reactions