forked from github/dev
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworkshopt_gpt.py
133 lines (104 loc) · 4.12 KB
/
workshopt_gpt.py
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
import os
import streamlit as st
import openai
from elasticsearch import Elasticsearch
# This code is part of an Elastic Blog showing how to combine
# Elasticsearch's search relevancy power with
# OpenAI's GPT's Question Answering power
# https://www.elastic.co/blog/chatgpt-elasticsearch-openai-meets-private-data
# Code is presented for demo purposes but should not be used in production
# You may encounter exceptions which are not handled in the code
# Required Environment Variables
# openai_api - OpenAI API Key
# cloud_id - Elastic Cloud Deployment ID
# cloud_user - Elasticsearch Cluster User
# cloud_pass - Elasticsearch User Password
openai.api_key = os.environ['GP_API']
g_model = "gpt-3.5-turbo-0613"
# Connect to Elastic Cloud cluster
def es_connect(cid, user, passwd):
es = Elasticsearch(cloud_id=cid, basic_auth=(user, passwd))
return es
# Search ElasticSearch index and return body and URL of the result
def search(query_text):
cid = os.environ['cloud_id']
cp = os.environ['cloud_pass']
cu = os.environ['cloud_user']
es = es_connect(cid, cu, cp)
# Elasticsearch query (BM25) and kNN configuration for hybrid search
query = {
"bool": {
"must": [{
"match": {
"title": {
"query": query_text,
"boost": 1
}
}
}],
"filter": [{
"exists": {
"field": "title-vector"
}
}]
}
}
knn = {
"field": "title-vector",
"k": 1,
"num_candidates": 20,
"query_vector_builder": {
"text_embedding": {
"model_id": "sentence-transformers__all-distilroberta-v1",
"model_text": query_text
}
},
"boost": 2
}
fields = ["title", "body_content", "url"]
#index = 'search-cfp-docs'
index = 'search-my-llm-vector-search'
print(query)
print(knn)
resp = es.search(index=index,
query=query,
knn=knn,
fields=fields,
size=1,
source=False)
print ("==============resposta================")
print (resp)
print ("==============fim resposta================")
body = resp['hits']['hits'][0]['fields']['body_content'][0]
url = resp['hits']['hits'][0]['fields']['url'][0]
return body, url
def truncate_text(text, max_tokens):
tokens = text.split()
if len(tokens) <= max_tokens:
return text
return ' '.join(tokens[:max_tokens])
# Generate a response from ChatGPT based on the given prompt
def chat_gpt(prompt, model="gpt-3.5-turbo", max_tokens=1024, max_context_tokens=4000, safety_margin=900):
# Truncate the prompt content to fit within the model's context length
truncated_prompt = truncate_text(prompt, max_context_tokens - max_tokens - safety_margin)
response = openai.ChatCompletion.create(model=model,
messages=[{"role": "system", "content": "You are a helpful assistant."}, {"role": "user", "content": truncated_prompt}])
return response["choices"][0]["message"]["content"]
st.title("Workshop GPT")
# Main chat form
with st.form("chat_form"):
query = st.text_input("You: ")
submit_button = st.form_submit_button("Send")
# Generate and display response on form submission
negResponse = "I'm unable to answer the question based on the information I have from Elastic."
if submit_button:
resp, url = search(query)
prompt = f"Responda a esta pergunta: {query}\nUtilizando apenas as informações do Elastic: {resp}\nSe a resposta não estiver contida no documento fornecido, responda '{negResponse}' e nada mais."
prompt = f"Responda a esta pergunta: {query}\nUtilizando apenas as informações do Elastic: {resp}"
answer = chat_gpt(prompt, g_model)
print("prompt------->", prompt)
print("answer------->", answer)
if negResponse in answer:
st.write(f"ChatGPT: {answer.strip()}")
else:
st.write(f"ChatGPT: {answer.strip()}\n\nDocs: {url}")