-
Notifications
You must be signed in to change notification settings - Fork 13
/
questions.rb
62 lines (47 loc) · 1.62 KB
/
questions.rb
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
require 'dotenv'
require 'ruby/openai'
require 'csv'
require 'cosine_similarity'
Dotenv.load()
openai = OpenAI::Client.new(access_token: ENV['OPENAI_API_KEY'])
puts "Welcome to the Sterling Parts AI Knowledge Base. How can I help you?"
question = gets
response = openai.embeddings(
parameters: {
model: "text-embedding-ada-002",
input: question
}
)
question_embedding = response['data'][0]['embedding']
similarity_array = []
CSV.foreach("embeddings.csv", headers: true) do |row|
text_embedding = JSON.parse(row['embedding'])
similarity_array << cosine_similarity(question_embedding, text_embedding)
end
index_of_max = similarity_array.index(similarity_array.max)
original_text = ""
CSV.foreach("embeddings.csv", headers: true).with_index do |row, rowno|
if rowno == index_of_max
original_text = row['text']
end
end
prompt =
"You are an AI assistant. You work for Sterling Parts which is a car parts online store located in Australia.
You will be asked questions from a customer and will answer in a helpful and friendly manner.
You will be provided company information from Sterline Parts under the [Article] section. The customer question
will be provided unders the [Question] section. You will answer the customers questions based on the article.
If the users question is not answered by the article you will respond with 'I'm sorry I don't know.'
[Article]
#{original_text}
[Question]
#{question}"
response = openai.completions(
parameters: {
model: "text-davinci-003",
prompt: prompt,
temperature: 0.2,
max_tokens: 500,
}
)
puts "\nAI response:\n"
puts response['choices'][0]['text'].lstrip