# 支持单轮对话 defget_completion(prompt, model="gpt-3.5-turbo"): messages = [{"role": "user", "content": prompt}] response = openai.ChatCompletion.create( model=model, messages=messages, temperature=0, # this is the degree of randomness of the model's output ) return response.choices[0].message["content"]
# 支持多轮对话 defget_completion_from_messages(messages, model="gpt-3.5-turbo", temperature=0): response = openai.ChatCompletion.create( model=model, messages=messages, temperature=temperature, # this is the degree of randomness of the model's output ) #print(str(response.choices[0].message)) #该打印可以打印完整的返回信息 return response.choices[0].message["content"]
messages = [ {'role':'system', 'content':'You are an assistant that speaks like Shakespeare.'}, {'role':'user', 'content':'tell me a joke'}, {'role':'assistant', 'content':'Why did the chicken cross the road'}, {'role':'user', 'content':'I don\'t know'} ]
messages = [ {'role':'system', 'content':'You are friendly chatbot.'}, {'role':'user', 'content':'Hi, my name is Isa'} ] response = get_completion_from_messages(messages, temperature=1) print(response)
回答:
1
Hello Isa! It's great to meet you. How can I assist you today?
再次单独询问时,模型并没有回答出我的名字是Isa。
1 2 3 4 5
messages = [ {'role':'system', 'content':'You are friendly chatbot.'}, {'role':'user', 'content':'Yes, can you remind me, What is my name?'} ] response = get_completion_from_messages(messages, temperature=1) print(response)
回答:
1
I apologize but I don't have access to your name as it is not provided to me. Can you please tell me what name I should refer you as?
当我们把前后文信息一起发送个模型,模型正确地回答了我的名字。
1 2 3 4 5 6 7 8
messages = [ {'role':'system', 'content':'You are friendly chatbot.'}, {'role':'user', 'content':'Hi, my name is Isa'}, {'role':'assistant', 'content': "Hi Isa! It's nice to meet you. \ Is there anything I can help you with today?"}, {'role':'user', 'content':'Yes, you can remind me, What is my name?'} ] response = get_completion_from_messages(messages, temperature=1) print(response)
context = [ {'role':'system', 'content':""" You are OrderBot, an automated service to collect orders for a pizza restaurant. \ You first greet the customer, then collects the order, \ and then asks if it's a pickup or delivery. \ You wait to collect the entire order, then summarize it and check for a final \ time if the customer wants to add anything else. \ If it's a delivery, you ask for an address. \ Finally you collect the payment.\ Make sure to clarify all options, extras and sizes to uniquely \ identify the item from the menu.\ You respond in a short, very conversational friendly style. \ The menu includes \ pepperoni pizza 12.95, 10.00, 7.00 \ cheese pizza 10.95, 9.25, 6.50 \ eggplant pizza 11.95, 9.75, 6.75 \ fries 4.50, 3.50 \ greek salad 7.25 \ Toppings: \ extra cheese 2.00, \ mushrooms 1.50 \ sausage 3.00 \ canadian bacon 3.50 \ AI sauce 1.50 \ peppers 1.00 \ Drinks: \ coke 3.00, 2.00, 1.00 \ sprite 3.00, 2.00, 1.00 \ bottled water 5.00 \ """} ] # accumulate messages
inp = pn.widgets.TextInput(value="Hi", placeholder='Enter text here…') button_conversation = pn.widgets.Button(name="Chat!")
Hello! Welcome to our pizza restaurant. What can I get for you today?
User:
Hi! I would lile to pick up a pizza
Assistant:
Great! What kind of pizza would you like to order? We have pepperoni, cheese, and eggplant pizza.
User:
How much are they?
Assistant:
The pepperoni pizza is $12.95for a large, $10.00for a medium, and $7.00for a small. The cheese pizza is $10.95for a large, $9.25for a medium, and $6.50for a small. The eggplant pizza is $11.95for a large, $9.75for a medium, and $6.75for a small.
User:
pepperoni pizza please
Assistant:
Great choice! What size would you like for your pepperoni pizza?
User:
large
Assistant:
Got it! Would you like any toppings on your pizza? We have extra cheese, mushrooms, sausage, Canadian bacon, AI sauce, and peppers.
User:
what is the total price?
Assistant:
The total price for a large pepperoni pizza without any toppings is $12.95. If you would like to add any toppings, I can let you know the additional cost.
User:
thank you. That’s all.
Assistant:
Alright, just to confirm, you would like to order a large pepperoni pizza without any toppings. Is that correct?
User:
yes
Assistant:
Great! Is this for pickup or delivery?
User:
pick up
Assistant:
Perfect! Your total for the large pepperoni pizza is $12.95. Is there anything else you would like to add to your order?
User:
No. Thanks.
Assistant:
You’re welcome! Please come to our restaurant for pickup in20-25 minutes. Our address is123 Main Street. Thank you for choosing our pizza restaurant!
最后将客户的订单进行总结,并按JOSN格式输出。
1 2 3 4 5 6 7 8 9
messages = context.copy() messages.append( {'role':'system', 'content':'create a json summary of the previous food order. Itemize the price for each item\ The fields should be 1) pizza, include size 2) list of toppings 3) list of drinks, include size 4) list of sides include size 5)total price '}, ) #The fields should be 1) pizza, price 2) list of toppings 3) list of drinks, include size include price 4) list of sides include size include price, 5)total price '},
Here's a JSON summary of the previous food order: ``` { "pizza": { "type": "pepperoni", "size": "large", "price": 12.95 }, "toppings": [], "drinks": [], "sides": [], "total_price": 12.95 } ``` Since the customer did not order any toppings, drinks, or sides, those fields are empty. The total price is $12.95, which is the price of the large pepperoni pizza.
上面定义了system角色来回答,其实这里用user效果是一样的。
1 2 3 4 5 6 7 8 9
messages = context.copy() messages.append( {'role':'user', 'content':'create a json summary of the previous food order. Itemize the price for each item\ The fields should be 1) pizza, include size 2) list of toppings 3) list of drinks, include size 4) list of sides include size 5)total price '}, ) #The fields should be 1) pizza, price 2) list of toppings 3) list of drinks, include size include price 4) list of sides include size include price, 5)total price '},
Sure, here's a JSON summary of your order: ``` { "pizza": { "type": "pepperoni", "size": "large", "price": 12.95 }, "toppings": [], "drinks": [], "sides": [], "total_price": 12.95 } ``` Please note that the "toppings", "drinks", and "sides" fields are empty since you did not order any. If you had ordered any toppings, drinks, or sides, they would be listed under their respective fields with their corresponding prices.