Hi, I want my bot to get an answer from GPT-3 using custom action. any guidance will be helpful.
thanks.
Hi, I want my bot to get an answer from GPT-3 using custom action. any guidance will be helpful.
thanks.
Hello @shamspias ,
I hope this lines of code will help you.
import openai
from typing import Any, Text, Dict, List
from rasa_sdk import Action, Tracker
from rasa_sdk.events import SlotSet
from rasa_sdk.types import DomainDict
from rasa_sdk.executor import CollectingDispatcher
from rasa_sdk.events import (
SlotSet,
UserUtteranceReverted,
ConversationPaused,
EventType,
)
class ActionOpenaiChat(Action):
def name(self) -> Text:
return "action_openai_chat"
def run(
self,
dispatcher: CollectingDispatcher,
tracker: Tracker,
domain: DomainDict) -> List[EventType]:
openai.api_key = "insert api key from openAI"
response = openai.Completion.create(
model="text-davinci-002",
prompt="The following is a conversation with an AI assistant. The assistant is helpful, creative, clever and very friendly.\n\nHuman: Hello, who are you?\nAI: I am an AI created by OpenAI. How can I help you today?\nHuman: {}\nAI:".format(tracker.latest_message['text']),
temperature=0.9,
max_tokens=150,
top_p=1,
frequency_penalty=0.0,
presence_penalty=0.6,
stop=[" Human:", " AI:"]
)
if 'choices' in response.keys():
if len(response['choices']) > 0:
dispatcher.utter_message(text="{}".format(response['choices'][0]['text']))
else:
dispatcher.utter_message(text="Unfortunately, the internet doesn't have an answer to your question either.")
else:
dispatcher.utter_message(text="Unfortunately, the internet doesn't have an answer to your question either.")
return []