mirror of
https://github.com/langchain-ai/langchain.git
synced 2026-10-09 19:35:20 +03:00
2.5 KiB
2.5 KiB
In [11]:
from langchain.chat_models import ChatOpenAI
from langchain.prompts import ChatPromptTemplate, SystemMessagePromptTemplate, HumanMessagePromptTemplate
from langchain.schema.output_parser import StrOutputParser
from langchain.utilities import PythonREPLIn [12]:
template = """Write some python code to solve the user's problem.
Return only python code in Markdown format, e.g.:
```python
....
```"""
prompt = ChatPromptTemplate.from_messages(
[("system", template), ("human", "{input}")]
)
model = ChatOpenAI()In [13]:
def _sanitize_output(text: str):
_, after = text.split("```python")
return after.split("```")[0]In [14]:
chain = prompt | model | StrOutputParser() | _sanitize_output | PythonREPL().runIn [15]:
chain.invoke({"input": "whats 2 plus 2"})Out [15]:
Python REPL can execute arbitrary code. Use with caution.
'4\n'