gpt4 book ai didi

c# - 在 Azure 聊天机器人框架中使用聊天机器人响应作为触发词

转载 作者:太空宇宙 更新时间:2023-11-03 12:11:55 26 4
gpt4 key购买 nike

我正在使用 Azure 中的机器人框架开发一个聊天机器人,我目前正在尝试使用我的聊天机器人响应作为用户触发词将用户问题存储在表存储中,例如当机器人响应“I”时m 抱歉,我没有答案。请尝试重新表述您的问题”,它会记录用户的第一个问题,例如“我如何飞行?”。

任何有关此问题的帮助将不胜感激!

最佳答案

这是实现这一目标的一种方法。您可以将每个问题文本存储在字典中,如果查询未正确回答,则将其发送到永久存储。

首先,创建一个静态字典来保存值:

public static class Utils
{
public static Dictionary<string, string> MessageDictionary = new Dictionary<string, string>();
}

其次,在消息 Controller 中,当您的机器人收到每个用户的每条消息时,您可以存储它,如下所示:

    public async Task<HttpResponseMessage> Post([FromBody] Activity activity)
{
if (activity.Type == ActivityTypes.Message)
{
var userId = activity.From.Id;
var message = activity.Text;
if (!Utils.MessageDictionary.ContainsKey(userId))
{
ConnectorClient connector = new ConnectorClient(new System.Uri(activity.ServiceUrl));
var reply = activity.CreateReply();

//save all incoming messages to a dictionary
Utils.MessageDictionary.Add(userId, message);

// this can be removed it just confirms it was saved
reply.Text = $"Message saved {userId} - {Utils.MessageDictionary[userId]}";
await connector.Conversations.ReplyToActivityAsync(reply);
}
await Conversation.SendAsync(activity, () => new Dialogs.RootDialog());
}

else
{
HandleSystemMessage(activity);
}

var response = Request.CreateResponse(HttpStatusCode.OK);
return response;
}

Nest 创建一个继承自 IBotToUser 的类,该类可以在消息发送给用户之前拦截消息。如果返回给用户的文本是您提供的文本“对不起,我没有答案给您。请尝试重新表述您的问题”,我们会将消息保存到永久存储中:

public sealed class CustomBotToUser : IBotToUser
{
private readonly IBotToUser inner;
private readonly IConnectorClient client;

public CustomBotToUser(IBotToUser inner, IConnectorClient client)
{
SetField.NotNull(out this.inner, nameof(inner), inner);
SetField.NotNull(out this.client, nameof(client), client);
}


public async Task PostAsync(IMessageActivity message,
CancellationToken cancellationToken = default(CancellationToken))
{
if (message.Text == "I’m Sorry, I don’t have an answer for you. Please try and rephrase your question")
{
//save to permanant storage here
//if you would like to use a database
//I have a very simple database bot example here
//https://github.com/JasonSowers/DatabaseBotExample
}

//user is the recipient
var userId = message.Recipient.Id;

//remove entry from dictionary
Utils.MessageDictionary.Remove(userId);

//this is just for testing purposes and can be removed
try
{
await inner.PostAsync($"{userId} - {Utils.MessageDictionary[userId]}");
}
catch (Exception e)
{
await inner.PostAsync($"No entry found for {userId}");
}
await inner.PostAsync((Activity) message, cancellationToken);
}

public IMessageActivity MakeMessage()
{
return inner.MakeMessage();
}
}

您还需要使用 Autofac 在 Global.asax 中注册此类,使 Aplication_Start() 方法如下所示:

    protected void Application_Start()
{
GlobalConfiguration.Configure(WebApiConfig.Register);

Conversation.UpdateContainer(
builder =>
{
builder.RegisterModule(new AzureModule(Assembly.GetExecutingAssembly()));

// Bot Storage: Here we register the state storage for your bot.
// Default store: volatile in-memory store - Only for prototyping!
// We provide adapters for Azure Table, CosmosDb, SQL Azure, or you can implement your own!
// For samples and documentation, see: [https://github.com/Microsoft/BotBuilder-Azure](https://github.com/Microsoft/BotBuilder-Azure)
var store = new InMemoryDataStore();

// Other storage options
// var store = new TableBotDataStore("...DataStorageConnectionString..."); // requires Microsoft.BotBuilder.Azure Nuget package
// var store = new DocumentDbBotDataStore("cosmos db uri", "cosmos db key"); // requires Microsoft.BotBuilder.Azure Nuget package

builder.Register(c => store)
.Keyed<IBotDataStore<BotData>>(AzureModule.Key_DataStore)
.AsSelf()
.SingleInstance();

builder
.RegisterType<CustomBotToUser>()
.Keyed<IBotToUser>(typeof(LogBotToUser));
});
}

这是我分享的 Global.asax 代码的重要部分:

        builder
.RegisterType<CustomBotToUser>()
.Keyed<IBotToUser>(typeof(LogBotToUser));

关于c# - 在 Azure 聊天机器人框架中使用聊天机器人响应作为触发词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51790714/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com