mirror of
https://github.com/hwchase17/langchain
synced 2024-11-20 03:25:56 +00:00
27 lines
942 B
Plaintext
27 lines
942 B
Plaintext
```python
|
|
from langchain.chat_models import ChatOpenAI
|
|
from langchain.prompts.chat import ChatPromptTemplate
|
|
from langchain.schema import BaseOutputParser
|
|
|
|
class CommaSeparatedListOutputParser(BaseOutputParser):
|
|
"""Parse the output of an LLM call to a comma-separated list."""
|
|
|
|
|
|
def parse(self, text: str):
|
|
"""Parse the output of an LLM call."""
|
|
return text.strip().split(", ")
|
|
|
|
template = """You are a helpful assistant who generates comma separated lists.
|
|
A user will pass in a category, and you should generate 5 objects in that category in a comma separated list.
|
|
ONLY return a comma separated list, and nothing more."""
|
|
human_template = "{text}"
|
|
|
|
chat_prompt = ChatPromptTemplate.from_messages([
|
|
("system", template),
|
|
("human", human_template),
|
|
])
|
|
chain = chat_prompt | ChatOpenAI() | CommaSeparatedListOutputParser()
|
|
chain.invoke({"text": "colors"})
|
|
# >> ['red', 'blue', 'green', 'yellow', 'orange']
|
|
```
|