forked from langchain-ai/langgraph-example
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.py
More file actions
137 lines (99 loc) · 4.8 KB
/
example.py
File metadata and controls
137 lines (99 loc) · 4.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import openai
def analyze_feedback(feedback, business_goal):
"""
Analyze user feedback based on the selected business goal.
Parameters:
- feedback: String containing user feedback.
- business_goal: String specifying the selected business goal.
Returns:
- Boolean indicating whether the feedback is valid or not.
"""
# Construct the prompt for the LLM
prompt = f"Analyze this({feedback}) user feedback and determine its relevance on the basis of [{business_goal}]. " \
"If the feedback is not valid, just say 'invalid'; otherwise say 'valid'."
# Call the LLM API (Assuming using OpenAI's API as an example)
response = openai.Completion.create(
engine="text-davinci-003", # Replace with the appropriate model engine
prompt=prompt,
max_tokens=10,
temperature=0.3
)
# Extract the result from the LLM response
validity = response.choices[0].text.strip()
# Return True if the feedback is valid, otherwise False
return "valid" in validity.lower()
def identify_relevant_section(feedback, content):
"""
Identify the most relevant section of the content based on user feedback.
Parameters:
- feedback: String containing user feedback.
- content: String containing the entire content.
Returns:
- String of the most relevant section of the content.
"""
# Construct the prompt for the LLM with entity recognition
prompt = f"Analyze this({feedback}) user feedback and determine which part of this({content}) content" \
"it is referring to specifically. Extract the entity or phrase that connects the feedback to the content."
# Call the LLM API (Assuming using OpenAI's API as an example)
response = openai.Completion.create(
engine="text-davinci-003", # Replace with the appropriate model engine
prompt=prompt,
max_tokens=50,
temperature=0.3
)
# Extract the relevant section from the LLM response
extracted_entity = response.choices[0].text.strip()
return extracted_entity
def revise_content_based_on_feedback(content, feedback, business_value):
"""
Revise content based on user feedback in accordance with a specified business value.
Parameters:
- content: String containing the original content.
- feedback: String containing user feedback.
- business_value: String specifying the business value that revisions should adhere to.
Returns:
- String of the revised content.
"""
# Construct the prompt for the LLM
prompt = f"Please revise this({content}) on the basis of this({feedback}) in accordance with this({business_value}) value."
# Call the LLM API (Assuming using OpenAI's API as an example)
response = openai.Completion.create(
engine="text-davinci-003", # Replace with the appropriate model engine
prompt=prompt,
max_tokens=300, # Adjust token limit based on the expected length of revisions
temperature=0.3
)
# Extract the revised content from the LLM response
revised_content = response.choices[0].text.strip()
return revised_content
def process_feedback(content, feedback, business_goal):
"""
Process a user comment to determine relevance, identify the section it refers to, and revise the content.
Parameters:
- content: String containing the entire content of the page.
- feedback: String containing the user feedback.
- business_goal: String specifying the business goal.
Returns:
- String containing the revised content if relevant, otherwise a message indicating no action was taken.
"""
# Step 1: Analyze the feedback for relevance
is_relevant = analyze_feedback(feedback, business_goal)
if not is_relevant:
return "Feedback is not relevant to the business goal. No revision made."
# Step 2: Identify the most relevant section of the content
relevant_section = identify_relevant_section(feedback, content)
# Step 3: Revise the identified section based on the feedback and business goal
revised_section = revise_content_based_on_feedback(relevant_section, feedback, business_goal)
# Return the revised content or section
return revised_section
# Example usage
page_content = """
Our product is designed for efficiency and reliability, ensuring seamless integration with your existing systems.
We offer a wide range of services to meet your needs, with a focus on customer satisfaction.
The pricing model is designed to be flexible and accommodate businesses of all sizes.
"""
user_feedback = "The integration process is too complex and needs simplification."
business_goal = "improving ease of integration"
# Process the feedback and revise the content if relevant
revised_content = process_feedback(page_content, user_feedback, business_goal)
print(revised_content)