介绍
最近,我开始从事一个新项目,其中包括使用Google已经众所周知的服务(AI)DialogFlow。最重要的是,我们有一个可以与多个站点的API一起使用的机器人(Telegram),而DialogFlow本身则帮助我们构建了与人的通信,基本上就是为此而创建的。我必须说,这篇文章是为刚接触该服务或已经很少有经验的人准备的,因此可以使用以下术语:意图,上下文,动作,事件以及我们为实体而收集的内容。我希望本文对那些不太了解如何通过实体执行CRUD操作的人有用。DialogFlow中的实体
在DialogFlow中,实体是由ReferenceValue和Synonyms组成的一些实体。类似于键值,不同之处在于可以有很多同义词,并且同义词越多越好,因为它“简化了机器人的生活”,而且他更容易理解所谈论的是什么值以及对勾即使您错过一个字母或另一个符号,模糊匹配也将帮助机器人也了解危险所在。看起来像这样:莫斯科-莫斯科,莫斯科,莫斯科...结束短暂的旅行,我想补充一点,就是与实体有关的事情引起很多注意。当然,我并没有减少意图的含义,等等,但是事实仍然存在。实体类型
这是我的第一个尽管很小的陷阱。毕竟,文档中包含使用EntityType的方法,还有使用EntityType的方法-哪一个是什么?实际上,一切并没有那么复杂,只是由于在DialogFlow本身中带有EntityTypes的选项卡称为Entities:
EntityType:
Entitie:而引起混淆,
尽管DialogFlow本身认为存在Entity并具有Entries(即本质和条目)。让我们继续。您可以直接在DialoFlow上手动创建EntityType,但是我将展示如何通过代码完成此操作(毕竟,为了创建实体,我们需要知道它在哪个EntityType中)。首先,启用NuGet包Google.Cloud.Dialogflow.V2。在撰写本文时,已安装版本1.1.0:
首先,我们创建一个类并将其命名为EntityTypeManagement,其中将进行基本操作:using Google.Api.Gax;
using Google.Cloud.Dialogflow.V2;
public class EntityTypeManagement
{
public async Task Create(string projectId, string displayName, EntityType.Types.Kind kind =
EntityType.Types.Kind.Map)
{
var client = await EntityTypesClient.CreateAsync();
var entityType = new EntityType();
entityType.DisplayName = displayName;
entityType.Kind = kind;
var createdEntityType = client.CreateEntityType(
parent: new ProjectAgentName(projectId),
entityType: entityType
);
Console.WriteLine($"Created EntityType: {createdEntityType.Name}");
}
public async Task<PagedEnumerable<ListEntityTypesResponse,EntityType>> List(string projectId)
{
var client = await EntityTypesClient.CreateAsync();
var response = client.ListEntityTypes(
parent: new ProjectAgentName(projectId)
);
return response;
}
public async Task<string> GetEntityTypeId(string projectId,string targetEventTypeName)
{
var client = await EntityTypesClient.CreateAsync();
var response = client.ListEntityTypes(
parent: new ProjectAgentName(projectId)
);
string id = response.Where(x => x.DisplayName == targetEventTypeName).FirstOrDefault().Name;
string returningId = id.Replace($"projects/{projectId}/agent/entityTypes/", "");
return returningId;
}
public async Task Delete(string projectId, string entityTypeId)
{
var client = await EntityTypesClient.CreateAsync();
client.DeleteEntityType(new EntityTypeName(projectId, entityTypeId: entityTypeId));
Console.WriteLine($"Deleted EntityType: {entityTypeId}");
}
public async Task BatchDelete(string projectId, IEnumerable<string> entityTypeIds)
{
var client = await EntityTypesClient.CreateAsync();
var entityTypeNames = entityTypeIds.Select(
id => new EntityTypeName(projectId, id).ToString());
client.BatchDeleteEntityTypes(new ProjectAgentName(projectId),
entityTypeNames);
}
}
在这里必须马上说没有更新功能,只是因为创建已经执行了它的功能。现在我们有了使用EntityTypes的基本方法,让我们继续Entity并立即创建EntityManagement类:public class EntityManagement
{
using Google.Cloud.Dialogflow.V2;
public async Task Create(string projectId,
string entityTypeId,
string entityValue,
string[] synonyms)
{
var client = await EntityTypesClient.CreateAsync();
var entity = new EntityType.Types.Entity() { Value = entityValue};
entity.Synonyms.AddRange(synonyms);
var operation = await client.BatchCreateEntitiesAsync(
parent: new EntityTypeName(projectId, entityTypeId),
entities: new[] { entity }
);
operation.PollUntilCompleted();
}
public async Task CreateMany(string projectId,string entityTypeId, IEnumerable<Entity> entities)
{
var client = await EntityTypesClient.CreateAsync();
List<EntityType.Types.Entity> results = new List<EntityType.Types.Entity>();
foreach (var item in entities)
{
var entity = new EntityType.Types.Entity() { Value = item.value };
entity.Synonyms.AddRange(item.synonyms);
results.Add(entity);
}
var operation = await client.BatchCreateEntitiesAsync(
parent: new EntityTypeName(projectId, entityTypeId),
entities: results.ToArray());
operation.PollUntilCompleted();
}
public async Task Delete(string projectId, string entityTypeId, string entityValue)
{
var client = await EntityTypesClient.CreateAsync();
var operation = await client.BatchDeleteEntitiesAsync(
parent: new EntityTypeName(projectId, entityTypeId),
entityValues: new[] { entityValue }
);
Console.WriteLine("Waiting for the entity deletion operation to complete.");
operation.PollUntilCompleted();
Console.WriteLine($"Deleted Entity: {entityValue}");
}
public async Task<List<Entity>> List(string projectId, string entityTypeId)
{
var client = await EntityTypesClient.CreateAsync();
var entityType = await client.GetEntityTypeAsync(new EntityTypeName(
projectId, entityTypeId
));
List<Entity> EntitiesList = new List<Entity>();
foreach (var entity in entityType.Entities)
{
List<string> Synonyms = new List<string>();
foreach (var item in entity.Synonyms)
{
Synonyms.Add(item);
}
EntitiesList.Add(new Entity { value = entity.Value, synonyms = Synonyms.ToArray() });
Synonyms = null;
}
return EntitiesList;
}
}
添加将代表实体的模型:public class Entity
{
public string value { get; set; }
public string[] synonyms { get; set; }
}
完成后,所有基本的CRUD功能均已准备就绪。同样,返回到Update,它可以单独实现,但是根本不需要,因为Create也像Update一样工作。这些示例非常简单,我必须同意,这里仍然有一个“硬编码”,但是我希望本文对那些像我一样无法弄清楚如何通过代码正确执行CRUD操作的人有用。static void Main(string[] args)
{
EntityTypesManagement entityTypesManagement = new EntityTypesManagement();
EntityManagement entityManagement = new EntityManagement();
string ProjectId = "Your Project`s id";
string EntityName = "Your EntityName";
var entityId = await entityTypesManagement.GetEntityTypeId(ProjectId, EntityName);
Entity entity = new Entity{value = "1", synonyms = new[]{"1","1"}};
entityManagement.Create(ProjectId,entityId,entity);
}