gpt4 book ai didi

c# - 如何使用 ITranscriptLogger 和 TranscriptLoggerMiddleware 在 cosmos DB 中存储聊天记录

转载 作者:行者123 更新时间:2023-11-30 15:51:32 32 4
gpt4 key购买 nike

我想使用 ITranscriptLogger 和 TranscriptMiddelWare 将整个聊天记录存储在 cosmos DB 中,但我很难做到这一点。我读过this MS article ,但我想将其存储在 Cosmos DB 而不是 Blob 存储中。此外,我正在尝试在 Startup.cs 中而不是在 Bot.cs 中实例化转录日志,并且我已尝试根据 this answer 实现它。没有任何运气。也就是说,没有存储抄本,并且我的 Azure cosmos DB 中没有容器。感谢任何帮助和反馈。

代码:

我已经创建了 TranscriptStore 类,并按照引用的 SO 答案中的说明创建并添加了中间件:

CosmosTranscriptStore.cs

public class CosmosTranscriptStore : ITranscriptLogger
{
private CosmosDbStorage _storage;

public CosmosTranscriptStore(CosmosDbStorageOptions config)
{
_storage = new CosmosDbStorage(config);
}
public async Task LogActivityAsync(IActivity activity)
{
// activity only contains Text if this is a message
var isMessage = activity.AsMessageActivity() != null ? true : false;
if (isMessage)
{
// Customize this to save whatever data you want
var data = new
{
From = activity.From,
To = activity.Recipient,
Text = activity.AsMessageActivity().Text,
};
var document = new Dictionary<string, object>();
// activity.Id is being used as the Cosmos Document Id
document.Add(activity.Id, data);
await _storage.WriteAsync(document, new CancellationToken());
}
}
}

Startup.cs

 public class Startup
{

private const string CosmosServiceEndpoint = "MyCosmosServiceEndpoint";
private const string CosmosDBKey = "MyCosmosDBKey";
private const string CosmosDBDatabaseName = "MyCosmosDBDatabaseName";
private const string CosmosDBCollectionName = "Transcript-storage";

public Startup(IConfiguration configuration)
{
Configuration = configuration;
}

public IConfiguration Configuration { get; }

public void ConfigureServices(IServiceCollection services)
{
var config = new CosmosDbStorageOptions
{
AuthKey = CosmosDBKey,
CollectionId = CosmosDBCollectionName,
CosmosDBEndpoint = new Uri(CosmosServiceEndpoint),
DatabaseId = CosmosDBDatabaseName,
};

var transcriptMiddleware = new TranscriptLoggerMiddleware(new CosmosTranscriptStore(config));

services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);

// Create the Bot Framework Adapter.
services.AddSingleton<IBotFrameworkHttpAdapter, AdapterWithErrorHandler>();

// Create the bot as a transient. In this case the ASP Controller is expecting an IBot.
services.AddSingleton<MainDialog>();
services.AddTransient<IBot, WelcomeBot<MainDialog>>();

services.AddBot<WelcomeBot<MainDialog>>(options =>
{
var middleware = options.Middleware;
middleware.Add(transcriptMiddleware);
});

}

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseHsts();
}

app.UseDefaultFiles();
app.UseStaticFiles();

app.UseMvc();
}
}
}

最佳答案

我设法通过将转录存储中间件添加到适配器来解决这个问题,我可能应该在问这个问题之前从一开始就这样做,但我对 bot 框架和这种类型的编程都很陌生。我就是这样解决的:

Startup.cs

public void ConfigureServices(IServiceCollection services)
{
...

var config = new CosmosDbStorageOptions
{
AuthKey = CosmosDBKey,
CollectionId = CosmosDBAntoherCollectionName,
CosmosDBEndpoint = new Uri(CosmosServiceEndpoint),
DatabaseId = CosmosDBDatabaseName,
};

var transcriptMiddleware = new TranscriptLoggerMiddleware(new CosmosTranscriptStore(config));

services.AddSingleton(transcriptMiddleware);

...

}

AdapterWithErrorHandler.cs

public class AdapterWithErrorHandler : BotFrameworkHttpAdapter
{
public AdapterWithErrorHandler(TranscriptLoggerMiddleware transcriptMiddlewareStore, IConfiguration configuration, ILogger<BotFrameworkHttpAdapter> logger, ConversationState conversationState = null)
: base(configuration, logger)
{
Use(transcriptMiddlewareStore);

OnTurnError = async (turnContext, exception) =>
{
...
};
}
}

此外,如果要将整个聊天记录存储在一个文档/项目中,我强烈建议通过对话 ID 而不是事件 ID 将数据存储在 CosmosTranscriptStore 类中。原因是每个事件都有自己的 ID,因此在 Cosmos DB 中为每个事件创建一个新项目。

 public class CosmosTranscriptStore : ITranscriptLogger
{
...

public async Task LogActivityAsync(IActivity activity)
{
...

document.Add(activity.Conversation.Id, data);
await chatStorage.WriteAsync(document, new CancellationToken());

}
}

关于c# - 如何使用 ITranscriptLogger 和 TranscriptLoggerMiddleware 在 cosmos DB 中存储聊天记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57112168/

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