|
1 | 1 | from dotenv import load_dotenv |
2 | 2 | from langchain.prompts import ChatPromptTemplate |
3 | 3 | from langchain.schema.output_parser import StrOutputParser |
4 | | -from langchain.schema.runnable import RunnableableMap, RunnableBranch |
| 4 | +from langchain.schema.runnable import RunnableBranch |
5 | 5 | from langchain_openai import ChatOpenAI |
6 | 6 |
|
7 | 7 | # Load environment variables from .env |
|
14 | 14 | positive_feedback_template = ChatPromptTemplate.from_messages( |
15 | 15 | [ |
16 | 16 | ("system", "You are a helpful assistant."), |
17 | | - ("human", "Generate a thank you note for this positive feedback: {feedback}."), |
| 17 | + ("human", |
| 18 | + "Generate a thank you note for this positive feedback: {feedback}."), |
18 | 19 | ] |
19 | 20 | ) |
20 | 21 |
|
21 | 22 | negative_feedback_template = ChatPromptTemplate.from_messages( |
22 | 23 | [ |
23 | 24 | ("system", "You are a helpful assistant."), |
24 | | - ("human", "Generate a response addressing this negative feedback: {feedback}."), |
| 25 | + ("human", |
| 26 | + "Generate a response addressing this negative feedback: {feedback}."), |
25 | 27 | ] |
26 | 28 | ) |
27 | 29 |
|
|
45 | 47 | ] |
46 | 48 | ) |
47 | 49 |
|
48 | | - |
49 | | -# Define the branch conditions based on feedback sentiment |
50 | | -def is_positive(feedback): |
51 | | - return "good" in feedback.lower() or "excellent" in feedback.lower() |
52 | | - |
53 | | - |
54 | | -def is_negative(feedback): |
55 | | - return "bad" in feedback.lower() or "poor" in feedback.lower() |
56 | | - |
57 | | - |
58 | | -def is_neutral(feedback): |
59 | | - return "okay" in feedback.lower() or "neutral" in feedback.lower() |
60 | | - |
| 50 | +# Define the feedback classification template |
| 51 | +classification_template = ChatPromptTemplate.from_messages( |
| 52 | + [ |
| 53 | + ("system", "You are a helpful assistant."), |
| 54 | + ("human", |
| 55 | + "Classify the sentiment of this feedback as positive, negative, neutral, or escalate: {feedback}."), |
| 56 | + ] |
| 57 | +) |
61 | 58 |
|
62 | 59 | # Define the runnable branches for handling feedback |
63 | 60 | branches = RunnableBranch( |
64 | | - (lambda x: is_positive(x), positive_feedback_template | model | StrOutputParser()), |
65 | | - (lambda x: is_negative(x), negative_feedback_template | model | StrOutputParser()), |
66 | | - (lambda x: is_neutral(x), neutral_feedback_template | model | StrOutputParser()), |
67 | | - escalate_feedback_template | model | StrOutputParser(), |
| 61 | + ( |
| 62 | + lambda x: "positive" in x, |
| 63 | + positive_feedback_template | model | StrOutputParser() # Positive feedback chain |
| 64 | + ), |
| 65 | + ( |
| 66 | + lambda x: "negative" in x, |
| 67 | + negative_feedback_template | model | StrOutputParser() # Negative feedback chain |
| 68 | + ), |
| 69 | + ( |
| 70 | + lambda x: "neutral" in x, |
| 71 | + neutral_feedback_template | model | StrOutputParser() # Neutral feedback chain |
| 72 | + ), |
| 73 | + escalate_feedback_template | model | StrOutputParser() |
68 | 74 | ) |
69 | 75 |
|
70 | | -# Create the combined chain using LangChain Expression Language (LCEL) |
71 | | -chain = branches |
| 76 | +# Create the classification chain |
| 77 | +classification_chain = classification_template | model | StrOutputParser() |
| 78 | + |
| 79 | +# Combine classification and response generation into one chain |
| 80 | +chain = classification_chain | branches |
72 | 81 |
|
73 | 82 | # Run the chain with an example review |
74 | 83 | # Good review - "The product is excellent. I really enjoyed using it and found it very helpful." |
75 | 84 | # Bad review - "The product is terrible. It broke after just one use and the quality is very poor." |
76 | 85 | # Neutral review - "The product is okay. It works as expected but nothing exceptional." |
77 | 86 | # Default - "I'm not sure about the product yet. Can you tell me more about its features and benefits?" |
78 | 87 |
|
79 | | -review = ( |
80 | | - "The product is terrible. It broke after just one use and the quality is very poor." |
81 | | -) |
82 | | -result = chain.invoke(review) |
| 88 | +review = "The product is terrible. It broke after just one use and the quality is very poor." |
| 89 | +result = chain.invoke({"feedback": review}) |
83 | 90 |
|
84 | 91 | # Output the result |
85 | 92 | print(result) |
0 commit comments