GPT3 fine tuned model returns additional questions and answers - openai-api

I have fine tuned a custom dataset using GPT3. I created a simple program to take user input (a question) and return the correct response. The program works, however it returns additional question and answers from the dataset I uploaded to the model.
I tried to reduce the max tokens cap and have set the temperature to 0, but I cannot seem to figure out how to stop the program from returning the additional questions and answers. Has anyone encountered this problem and if so how can I fix it?
Here is my code:
import openai
openai.api_key = "MY_API_KEY"
def respond(prompt):
completions = openai.Completion.create(
engine="MY_FINED_TUNED_MODEL",
prompt=prompt,
max_tokens=50,
n=1,
stop=None,
temperature=0,
)
message = completions.choices[0].text
return message
while True:
prompt = input("Enter your question: ")
if prompt.lower() == "end":
break
response = respond(prompt)
print(response)

You can handle this by providing more explicit details to the prompt and even providing one-shot or many-shot examples in the prompt.
For example say this was a Q&A bot for a computer store:
question = 'Do you sell computers?'
prompt = f"""Act as a question and answer AI. You will be provided with a question and you should respond based on your fine-tuning data. Provide the most appropriate answer to the question. Provide a single answer. Do not provide additional questions and answers.
Example question:
Do you sell cars?
Example response:
Sorry, we are a computer retailer and only sell computers.
Question:
{question}
Response:"""
Depending on your responses from the model you can also use a stop sequence. For example using \r\n will cause the model to stop responding when it generates a new line.
If you provide more details of the types of questions and responses you are getting from the model I can provide you a better prompt.

Related

Dialogflow: Invoke Follow-up Intent without adding training data

I am trying to build a questions and answers app in Dialogflow. The problem I am stuck is in asking user questions in sequence.
The tricky part is that we have 40 questions saved in Google Cloud SQL and out of these, the cloud function sends 1 random question.
Now the first follow-up intent with first random question is successfully triggered.
The problem we are facing is in calling the second or further follow-up intent. Since the questions are asked using fulfillment API, hence we are not sure what will be the next question and its answer.
We tried to build a follow-up sequence but it didn't worked. Following is the attached screenshot for reference.
Seeking help. Thanks in Advance.
You can go to the next intent having trained with a random user input by using context linking and #sys.any Entity in Dialogflow which is used to get the user input i.e Training Phrases in Dialogflow having any random phrases or text.
Image reference for Entity.
Image reference for context linking of a question
Image reference for context linking with previous question

Moving to next question when failing to receive a response

We had a developer to come up with a prototype for a bot for bookkeeping questions and we understand that the bot is not perfect. Our biggest challenge was to ensure that after 2-3 failed attempts for the bot to receive an appropriate response, the bot moves on to the next question and that's it. Our previous developer claimed that it's not possible, is that true or not? Currently the bot just gives up after a couple of attempt and that's it.
I am not a tech person and I would really appreciate some help on this.
Hypothetical example of the ideal scenario:
Q: What accounting software do you use?
A: askdnjsajld
Q: Didn't get that. What accounting software do you use?
A: asdkjnajksdn
Q: I am sorry, didn't get that. Let's move on to the next question... When would you like to receive your financials?
A: month-end
Thank you for your help!
Yes, this is possible, although the exact details of how you do this depend on how you're handling responses from the user in the first place.
The most important thing to keep in mind to handle this, however, is to remember that Intents represent what the user says and not how you handle it or how you reply.
That last bit gives the simplest answer to your question - you can, of course, reply however you want the bot to reply after each round. It sounds like you were able to implement logic that says if it got a result it didn't want - you can extend that to add a counter that just uses the next question as its reply after a number of tries.
The more difficult part is to make sure that you know what question the user is replying to, and to make sure you capture the right value in case they try to go back and answer a previous question.
The general solution to this problem is twofold:
Have one Intent that accepts the direct answer to the question you're currently on, but have it triggerable only with certain Input Contexts being set. Then set that Context to be valid when you ask the question, and remove the Context when the question is answered.
Have other Intents that specifically respond to how the user might phrase the question if they were going back to answer it.
For example, if you have asked "What software do you use?" you might set the Context "ask-software-used". You would then have two Intents:
One with an Input Context of "ask-software-used" that accepts just valid software names.
Another with no Input Context, but which might have training phrases such as
"I'm using XXXX software"
"We are working with the XXXX package"
once the user does answer the question, clear the "ask-software-used" Context, ask the next question, and set its Context.
You can also use this to keep track of how many times you've had to repeat the question, waiting for an answer. If that counter hits the limit, do the same thing: clear the Context, ask the next question, and set its Context.

How to add specific answers to specific questions in dialogflow?

I'm new to using Dialogflow,and I want to create a simple DialogFlow bot that can answer basic addition,subtraction,multiplication,division questions.How would I code it that it responds to the specific question asked by the user? For example if I made a math intent, I used the training phase "What's 2 x 3", and I made the response "6". Now, I want to add more training phases and I need the bot to use the correct response. Also, another problem is that it would take an impossible amount of time to teach it every possible math question,so is there code I could use to change that?
The easiest way to be able to answer every combination of math question would be by using a fulfillment webhook. Here you can use code to do the calculations based on the user input. You could create an addition intent where you train the bot to recognize addition input and you would connect it to code in your webhook which can do additions and return the response. You can then also add intents for subtracting, multiplication and divisions and connect each of those intents to code which can do the math.
For the setup, you have two options. You can write code in the inline editor in Dialogflow or host your code in your own server and connect dialogflow to the url of that server. More info on that can be found here.

What is the right approach for handling direct and contextual questions in dialogflow?

I am creating a HR chatbot using Dialogflow. I am unable to figure out the right approach to have the bot handle both direct questions and questions asked in a contextual manner. For example:
Contextual case:
User: I want to know how many leaves i can get in a year
Bot: You get x number of leaves
User: Ok cool how do i apply for one then?
Bot: Follow this process to apply for a leave
Direct case (2 separate conversations with direct questions):
Conversation 1:
User: I want to know how many leaves i can get in a year
Bot: You get x number of leaves
Conversation 2:
User: I want to know how to apply for leave
Bot: Follow this process to apply for a leave
Approaches I have tried:
1) Adding input and output contexts to handle contextual cases and add direct questions to knowledge base.
The issue with this approach is that since we cannot give multiple phrases in knowledge base, most direct questions do not match
2) Have 2 intents, one with input and output contexts and one to handle direct questions. (For example: One intent would be leaves.apply.context which would have both input and output context set and would have training phrases like how do i apply for this and another intent leaves.apply.direct which would have training phrases like how do i apply for a leave and no context). I'm not convinced that this is the right way as I am essentially creating two intents for the same question with the same response.
So is there a recommended approach to solve this problem?
I don't think there is a recommended approach to this, it is a matter of taste. Your solutions are a nice way of approaching this, but I don't think you will get around the fact that you will have to make an extra intent with the same response if you want to allow a follow-up question to also be approachable directly due to the context.
If you really don't like to create two intents for the same response, I think you would be able to workout this scenario by creating two intents and dropping the contextual flow. Just create a HowManyLeavesAvailableIntent and a HowToApplyForLeaveIntent without any context and train the HowToApplyForLeaveIntent to trigger on phrases that would follow-up HowManyLeavesAvailableIntent and direct questions for HowToApplyForLeaveIntent. Due to the missing context, this might not be ideal because it can create weird mappings to intents, but it would allow you to have just one intent for how to apply for leave.

Google Action Intent with multiple input parameters

I'm looking to POC a small Google Action that gives a decision based on a few yes/no answers the user must answer first. Effectively I need to:
Ask a question
Store the result
Ask the next question
Store results
* Repeat until all yes/no answers given then end the conversation with a decision using saved values in the conversation.
Ongoing though I would like to add help to any of the questions. So the user could say "I don't understand", "Can you give an example", "Help" and it would give an example to help the user answer yes or no to the question they are up to.
After playing around through the labs it looks like I would do this by creating an Intent for my end decision and then nest a bunch of follow up intents within to gather all my yes/no answers. I feel this would get messy though as it would be a huge chain of them.
Is there a better way to design it?
Yes, using Followup Intents would be messy. It is almost never the right approach to the problem. Remember that Intents capture what the user has said and not what you are doing with what they have said.
If the questions are truly just yes/no, I would setup six Intents:
However you trigger the start of the questioning. This might be your Welcome Intent, or it may be something else.
Saying "yes" and equivalent
Saying "no" and equivalent
Asking for help
Asking to repeat the question
A fallback Intent that handles other unexpected input
When questioning begins, your fulfillment would setup a Context that contains the current question being asked and the responses for the questions so far. Answering yes or no would update the responses, determine what question to ask next, save this in the context, and ask it. Help, repeat, and the fallback Intent would respond with appropriate information based on the current question.

Resources