Skip to content

Instantly share code, notes, and snippets.

@amixpal
Last active January 16, 2024 00:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amixpal/55db20a647cbf7bceac96028e508585a to your computer and use it in GitHub Desktop.
Save amixpal/55db20a647cbf7bceac96028e508585a to your computer and use it in GitHub Desktop.
import openai
from langchain.llms import OpenAI
from langchain.chains import Continuation
from langchain.conversation import ConversationContext
# Set your OpenAI API key
openai.api_key = 'your-api-key'
# Initialize LangChain with OpenAI's GPT model
llm = OpenAI()
# Initialize a ConversationContext object
conversation_context = ConversationContext()
# Function to interpret the user's service preference
def interpret_service(user_input):
prompt = f"The user is interested in '{user_input}'. Which service does this refer to: dancing, swimming, or sports?"
response = llm(prompt).strip().lower()
if response not in ['dancing', 'swimming', 'sports']:
return None
else:
return response
# Function to interpret the user's time preference
def interpret_time_preference(user_input):
prompt = f"The user mentioned '{user_input}'. Is this referring to morning, day, or night time?"
response = llm(prompt).strip().lower()
if response not in ['morning', 'day', 'night']:
return None
else:
return response
# Function to interpret the user's setting preference
def interpret_setting(user_input):
prompt = f"The user said '{user_input}'. Are they referring to a personal or group setting?"
response = llm(prompt).strip().lower()
if response not in ['personal', 'group']:
return None
else:
return response
# Function to generate prompts for GPT based on the current context
def generate_prompt(conversation_context):
prompt = "As an intelligent assistant, I'm guiding the user about our services: dancing, swimming, and sports. " \
"I need to understand their preferences regarding service type, time (morning, day, night), " \
"and setting (personal or group). Here is our conversation so far:\n\n"
prompt += conversation_context.get_full_conversation()
prompt += "\nUser: "
return prompt
# Function to update the conversation context based on user responses
def update_context(response, conversation_context):
service_preference = interpret_service(response)
if service_preference:
conversation_context.update('service', service_preference)
time_preference = interpret_time_preference(response)
if time_preference:
conversation_context.update('time_preference', time_preference)
setting_preference = interpret_setting(response)
if setting_preference:
conversation_context.update('group_setting', setting_preference)
# Function to manage the conversation with conditional logic
def converse(conversation_context):
while not conversation_context.all_filled(['service', 'time_preference', 'group_setting']):
prompt = generate_prompt(conversation_context)
user_input = input("User: ")
conversation_context.add_user_input(user_input)
need_more_info = False
service_preference = interpret_service(user_input)
if service_preference is None:
print("AI: I'm not sure which service you are referring to. Could you specify if it's dancing, swimming, or sports?")
need_more_info = True
elif service_preference:
conversation_context.update('service', service_preference)
if not need_more_info:
time_preference = interpret_time_preference(user_input)
if time_preference is None:
print("AI: I'm not sure about the time preference you mentioned. Could you specify if it's morning, day, or night?")
need_more_info = True
elif time_preference:
conversation_context.update('time_preference', time_preference)
if not need_more_info:
setting_preference = interpret_setting(user_input)
if setting_preference is None:
print("AI: Could you clarify if you prefer a personal or group setting?")
need_more_info = True
elif setting_preference:
conversation_context.update('group_setting', setting_preference)
if not need_more_info:
continuation = Continuation(llm)
response = continuation(prompt + user_input)
conversation_context.add_bot_output(response)
print("AI: ", response)
update_context(response, conversation_context)
print("Collected Information:", conversation_context.context)
print("We are finding the best services for you based on your preferences.")
# Run the conversation
converse(conversation_context)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment