-
Notifications
You must be signed in to change notification settings - Fork 6
/
08-all-togheter.sql
139 lines (127 loc) · 4.26 KB
/
08-all-togheter.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
declare @text nvarchar(max) = 'What are some good products to organize a birthday party for teenager boy?'
declare @top int = 50
declare @min_similarity decimal(19,16) = 0.75
declare @retval int, @response nvarchar(max);
declare @payload nvarchar(max);
set @payload = json_object('input': @text);
-- Call to OpenAI to get the embedding of the search text
begin try
exec @retval = sp_invoke_external_rest_endpoint
@url = '<OPENAI_URL>/openai/deployments/embeddings/embeddings?api-version=2023-03-15-preview',
@method = 'POST',
@credential = [<OPENAI_URL>],
@payload = @payload,
@response = @response output;
end try
begin catch
select
'SQL' as error_source,
error_number() as error_code,
error_message() as error_message
return;
end catch
if (@retval != 0) begin
select
'OPENAI' as error_source,
json_value(@response, '$.result.error.code') as error_code,
json_value(@response, '$.result.error.message') as error_message,
@response as error_response
return;
end;
drop table if exists #r;
create table #r (response nvarchar(max));
insert into #r (response) values (@response);
-- Similarity Search
drop table if exists #s;
with cteVector as
(
select
cast([key] as int) as [vector_value_id],
cast([value] as float) as [vector_value]
from
#r
cross apply
openjson(json_query(response, '$.result.data[0].embedding'))
),
cteSimilar as
(
select
v2.[id],
sum(v1.[vector_value] * v2.[vector_value]) as cosine_similarity
from
cteVector v1
inner join
dbo.[walmart_ecommerce_product_details_embeddings_vectors] v2 on v1.vector_value_id = v2.vector_value_id
group by
v2.[id]
)
select top(@top)
r.cosine_similarity,
p.id,
p.[Product_Name],
p.[Description],
p.Category
into
#s
from
cteSimilar r
inner join
dbo.[walmart_ecommerce_product_details] p on r.[id] = p.[id]
where
cosine_similarity >= @min_similarity
order by
r.cosine_similarity desc;
;
declare @payload2 nvarchar(max);
select
@payload2 = string_agg(cast(id as varchar(10)) +'=>' + [Product_Name] + '=>' + [Description], char(13) + char(10))
from
#s;
set @payload2 =
json_object(
'messages': json_array(
json_object(
'role':'system',
'content':'
You as a system assistant who helps users find ideas to organize birthday parties using the products that are provided to you.
Products will be provided in an assistant message in the format of "Id=>Product=>Description". You can use this information to help you answer the user''s question.
'
),
json_object(
'role':'user',
'content': '## Source ##
' + @payload2 + '
## End ##
You answer needs to be a json object with the following format.
{
"answer": // the answer to the question, add a source reference to the end of each sentence. Source referece is the product Id.
"products": // a comma-separated list of product ids that you used to come up with the answer.
"thoughts": // brief thoughts on how you came up with the answer, e.g. what sources you used, what you thought about, etc.
}'
),
json_object(
'role':'user',
'content': + @text
)
),
'max_tokens': 800,
'temperature': 0.7,
'frequency_penalty': 0,
'presence_penalty': 0,
'top_p': 0.95,
'stop': null
);
exec @retval = sp_invoke_external_rest_endpoint
@url = '<OPENAI_URL>/openai/deployments/gpt-4-32k/chat/completions?api-version=2023-07-01-preview',
@headers = '{"Content-Type":"application/json"}',
@method = 'POST',
@credential = [<OPENAI_URL>],
@timeout = 120,
@payload = @payload2,
@response = @response output;
drop table if exists #j;
select * into #j from openjson(@response, '$.result.choices') c;
select [key], [value] from openjson((
select t.value from #j c cross apply openjson(c.value, '$.message') t
where t.[key] = 'content'
))