python - 在action_default_fallback中调用Chatgpt

标签 python rasa chatgpt-api

我试图让 chatgpt 在所有后备情况下代表我的 Rasa 机器人进行应答,但此代码导致无法识别“action_default_fallback”。我已在domains.yml中注册了此操作

有人可以帮忙吗?

配置.yml

 pipeline:
  - name: FallbackClassifier
    threshold: 0.7
    ambiguity_threshold: 0.1

policies:
  - name: RulePolicy
    core_fallback_threshold: 0.4
    core_fallback_action_name: "action_default_fallback"
    enable_fallback_prediction: True

Action .py

class ActionDefaultFallback(Action):
    def name(self) -> Text:
        return "action_default_fallback"

    def run(
        self,
        dispatcher: CollectingDispatcher,
        tracker: Tracker,
        domain: Dict[Text, Any],
    ) -> List[Dict[Text, Any]]:
    
    # Get user message from Rasa tracker
        user_message = tracker.latest_message.get('text')

    # def get_chatgpt_response(self, message):
        url = 'https://api.openai.com/v1/chat/completions'
        headers = {
            'Authorization': 'Bearer sk-xxxxxxxxxxxxxxxxxxxxxxXD',
            'Content-Type': 'application/json'
        }
        data = {
            'model': "gpt-3.5-turbo",
            'messages': [{'role': 'user', 'content': 'You: ' + user_message}],
            'max_tokens': 100
        }
        response = requests.post(url, headers=headers, json=data)
                # response = requests.post(api_url, headers=headers, json=data)

        if response.status_code == 200:
            ai = response.json()['choices'][0]['message']['content']
            dispatcher.utter_message(ai)
        else:
            # Handle error
            return "Sorry, I couldn't generate a response at the moment.

最佳答案

action_default_fallback 是一个内置操作,您可以重命名该操作并在 RulePolicyconfig.yml 以及 RulePolicy 中使用它您的故事/规则作为您的 FallbackClassifier 的操作。

关于python - 在action_default_fallback中调用Chatgpt,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76031421/

相关文章:

python - 使用 GEKKO 时无法访问服务器。到底是怎么回事?

python - Pandas 数据框数据透视表和分组

Flutter chat_gpt_sdk 错误 - 上传的图像必须是 png 且小于 4 mb

php - OpenAI ChatGPT (GPT-3.5) API : Why am I not getting a response if the stream parameter is set to false?

python - OpenAI API错误: "The model ' curie:ft-personal-2023-09-15-06-05-0 1' does not exist" when trying to delete a fine-tuned model

python - 元组到 DataFrame 转换的列表

python - 替换单引号 (') with double quotes (") 除了撇号

wikipedia-api - 拉萨 : ModuleNotFoundError: No module named 'wikipedia'

python-3.x - 如何减少 rasa 3 python 中的模型加载时间

machine-learning - 是否应该从 Rasa NLU 训练数据中删除标点符号?