房间映射-AI是概念验证(POC)Web API,可让用户找到最佳的酒店房间名称匹配。
此应用程序中最关键的成分是OpenAI嵌入SDK和PostgreSQL数据库与向量扩展的组合。
提供新的酒店房间名称后,API从OpenAI API中检索嵌入矢量,并将其存储在PostgreSQL数据库中。
然后,它使用PostgreSQL中的本机搜索功能来根据距离向量相似性找到潜在的匹配。
Web API和Postgres数据库都在容器中运行,除了Docker以外,都无需安装任何内容。
用于计算OpenIA侧嵌入的模型为:
AdaTextEmbedding
Openai的文本嵌入量衡量文本字符串的相关性。嵌入通常用于:
搜索(在其中通过与查询字符串相关的结果对结果进行排名)
嵌入是浮点数的向量(列表)。两个向量之间的距离测量其相关性。
较小的距离表明高相关性和较大的距离表明低相关性。
克隆存储库:https://github.com/brunonuzzi/room-mapping-ai
创建打开的AI密钥:https://platform.openai.com/account/api-keys
导航到项目目录: cd room-mapping-ai
用您自己的打开API键替换房间映射-AI/AppSettings.json。
"OpenAI" : {
"ApiKey" : " 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 "
}使用Docker构建Docker容器: docker-compose build
运行Docker组成: docker-compose up
导航到http:// localhost:5000/swagger/index.html
停止和卸下容器:
docker-compose down --remove-orphans
要使用房间映射AI API,请发送带有酒店房间名称的邮政请求作为请求主体
curl -X ' POST '
' http://localhost:5000/api/RoomMapping/GetMostSimilarRooms?roomName=Serenity%20Luxury '
-H ' accept: text/plain '
-d ' 'API将以潜在匹配的列表做出回应:
[
{
"id" : 1 ,
"hotelName" : " Mallorca Rocks " ,
"roomName" : " Serenity Suite " ,
"vectorDistance" : 0.06337405572419863
},
{
"id" : 10 ,
"hotelName" : " Mallorca Rocks " ,
"roomName" : " Serenity Double Room " ,
"vectorDistance" : 0.08145154924929454
},
{
"id" : 5 ,
"hotelName" : " Mallorca Rocks " ,
"roomName" : " Royal Executive Suite " ,
"vectorDistance" : 0.15970261195384094
},
{
"id" : 4 ,
"hotelName" : " Mallorca Rocks " ,
"roomName" : " Urban Escape Penthouse " ,
"vectorDistance" : 0.16005893649310143
},
{
"id" : 2 ,
"hotelName" : " Mallorca Rocks " ,
"roomName" : " Ocean View Deluxe " ,
"vectorDistance" : 0.16867594238009598
}
]打开您首选的PostgreSQL客户端(PSQL,PGADMIN或DBEAVER),并创建一个新连接:
localhost5432roommapping-userroommapping-passwordroom-mapping房间映射-AI带有预种植的数据,可帮助您快速入门。这些数据包括一系列酒店房间名称,其各自的嵌入向量。为了探索预种植的数据并更好地了解应用程序的功能,您可以直接在PostgreSQL数据库上执行示例查询。
例如,您可以使用以下查询找到与房间ID = 1(Serenity Suite)的前5个最相似的酒店房间(基于它们的嵌入矢量):
SELECT Id
,HotelName
,RoomName
,CreatedDate
, ' Serenity Suite ' SearchedRoomName
,embedding <=> ( select embedding from rooms where id = 1 )
as vector_distance
FROM public . Rooms
ORDER BY vector_distance limit 5<=>操作员在上一个查询的平均
cosine distance中,您可以在这里找到其他操作员
数据库将生成以下结果
| ID | 酒店名称 | 房间名称 | 创建日期 | 搜索房间名称 | vector_distance |
|---|---|---|---|---|---|
| 1 | 马洛卡岩石 | 宁静套房 | 2023-03-28 | 宁静套房 | 0 |
| 13 | 马洛卡岩石 | 宁静的奢侈品 | 2023-03-28 | 宁静套房 | 0.06337405572419863 |
| 10 | 马洛卡岩石 | 宁静双人间 | 2023-03-28 | 宁静套房 | 0.06572621072063112 |
| 5 | 马洛卡岩石 | 皇家行政套房 | 2023-03-28 | 宁静套房 | 0.12737531341984398 |
| 3 | 马洛卡岩石 | 花园露台房间 | 2023-03-28 | 宁静套房 | 0.1471648253749488 |
为了更好地了解播种方式,您可以检查seed.sql文件
要调试Visual Studio上的WEP API,请首先确保删除与Web API相关的所有现有容器,以避免移植冲突。
卸下容器后,只需在Visual Studio上打开room-mapping-ai.sln ,然后启动Docker Compose。
Web API应启动,并将附加调试器。
代码片段直接致电OpenAI API并生成酒店房间名称的嵌入向量
curl https://api.openai.com/v1/embeddings
-H " Content-Type: application/json "
-H " Authorization: Bearer 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 "
-d ' {
"input": "Ocean View Deluxe",
"model": "text-embedding-ada-002"
} '预期的结果:
{
"object" : " list " ,
"data" : [
{
"object" : " embedding " ,
"index" : 0 ,
"embedding" : [
0.0041769226 ,
0.0067320974 ,
...
]
}
],
"model" : " text-embedding-ada-002-v2 " ,
"usage" : {
"prompt_tokens" : 3 ,
"total_tokens" : 3
}
}房间映射的API利用OpenAI API生成嵌入向量。现在的价格为$ 0.0004/1K令牌
像“ Serenity Double Room”这样的平均字符串具有3个令牌。您可以在这里检查价格