Last active
May 26, 2024 09:07
-
-
Save adlumal/d5d1138b57011b0b61a20e83b7484377 to your computer and use it in GitHub Desktop.
GoalChain example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from goalchain import Field, ValidationError, Goal, GoalChain | |
import os | |
os.environ["OPENAI_API_KEY"] = "sk-ABC..." # SPECIFY OPENAI API KEY | |
def quantity_validator(value): | |
try: | |
value = int(value) | |
except (ValueError, TypeError): | |
raise ValidationError("Quantity must be a valid number") | |
if value <= 0: | |
raise ValidationError("Quantity cannot be less than one") | |
if value > 100: | |
raise ValidationError("Quantity cannot be greater than 100") | |
return value | |
class ProductOrderGoal(Goal): | |
product_name = Field("product to be ordered", format_hint="a string") | |
customer_email = Field("customer email", format_hint="a string") | |
quantity = Field("quantity of product", format_hint="an integer", validator=quantity_validator) | |
class OrderCancelGoal(Goal): | |
reason = Field("reason for order cancellation (optional)", format_hint="a string") | |
product_order_goal = ProductOrderGoal( | |
label="product_order", | |
goal="to obtain information on an order to be made", | |
opener="I see you are trying to order a product, how can I help you?", | |
out_of_scope="Ask the user to contact sales team at [email protected]" | |
) | |
order_cancel_goal = OrderCancelGoal( | |
label="cancel_current_order", | |
goal="to obtain the reason for the cancellation", | |
opener="I see you are trying to cancel the current order, how can I help you?", | |
out_of_scope="Ask the user to contact the support team at [email protected]", | |
confirm=False | |
) | |
product_order_goal.connect(goal=order_cancel_goal, | |
user_goal="to cancel the current order", | |
hand_over=True, | |
keep_messages=True) | |
order_cancel_goal.connect(goal=product_order_goal, | |
user_goal="to continue with the order anyway", | |
hand_over=True, | |
keep_messages=True) | |
goal_chain = GoalChain(product_order_goal) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment