-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrcai_functions.py
227 lines (186 loc) · 7.84 KB
/
rcai_functions.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
import json
from langchain.document_loaders import PyPDFLoader
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain.embeddings.openai import OpenAIEmbeddings
from langchain.vectorstores import FAISS
# from langchain.llms import OpenAI
from langchain.chat_models import ChatOpenAI
from langchain import PromptTemplate
from langchain.chains import LLMChain
from langchain.schema import HumanMessage, AIMessage, ChatMessage
# from langchain.tools import format_tool_to_openai_functions
from dotenv import find_dotenv, load_dotenv
import openai
from functions import get_order_desc, show_log_entries, get_order_activities
import textwrap
load_dotenv(find_dotenv())
embeddings = OpenAIEmbeddings()
def create_vectordb_from_pdf(video_url: str) -> FAISS:
loader = PyPDFLoader(pdf_path)
pages = loader.load()
# pages = loader.load_and_split()
text_splitter = RecursiveCharacterTextSplitter(
chunk_size=1000, chunk_overlap=100)
docs = text_splitter.split_documents(pages)
db = FAISS.from_documents(docs, embeddings)
return db
function_descriptions_multiple = [
{
"name": "get_order_desc",
"description": "Describe a given Order looking at Order number",
"parameters": {
"type": "object",
"properties": {
"order_id": {
"type": "string",
"description": "The order number, e.g. GPQ-1234",
},
},
"required": ["order_id"],
},
},
{
"name": "show_log_entries",
"description": "Extract logs from NAS where Order number is present",
"parameters": {
"type": "object",
"properties": {
"order_id": {
"type": "string",
"description": "The order number, e.g. GPQ-1234",
},
"log_text": {
"type": "string",
"description": "Log lines extracted from NAS",
},
},
"required": ["order_id", "log_text"],
},
},
{
"name": "get_order_activities",
"description": "Get Order activity summary of a given order number",
"parameters": {
"type": "object",
"properties": {
"order_id": {
"type": "string",
"description": "The order number, e.g. GPQ-1234",
},
"symbol": {
"type": "string",
"description": "The symbol used to place the order, e.g. IBM",
},
"quantity": {
"type": "string",
"description": "Quanity of the order placed",
},
"side": {
"type": "string",
"description": "side of the order placed",
},
},
"required": ["order_id", "symbol", "quantity", "side"],
},
},
]
def get_response_from_query(db, query, k=4):
"""
text-davinci-003 can handle up to 4097 tokens. Setting the chunksize to 1000 and k to 4 maximizes
the number of tokens to analyze.
"""
docs = db.similarity_search(query, k=k)
docs_page_content = " ".join([d.page_content for d in docs])
# llm = OpenAI(temperature=0.7, model_name="gpt-3.5-turbo")
llm = ChatOpenAI(model="gpt-3.5-turbo-0613", temperature=0)
# Start a conversation with multiple requests
user_prompt=query
user_prompt1 = PromptTemplate(
input_variables=["question", "docs"],
template="""
You are a helpful assistant that that can answer questions about Application Production Support issues in Capital Markets
based on provided production incident data and relevant use cases.
Answer the following question: {question}
By searching the following usecase data: {docs}
When you are asked track a given an order number ( for example, GPQ-1234), describe that given order looking at order number
Provide additional details by extracting logs from NAS using order number.
Also provide activity summary of that order
When you are asked further to provide more details on Orders given an order number ( for example, XYZ-1234), get more details on that order
""",
)
# Returns the function of the first request
first_response = llm.predict_messages(
[HumanMessage(content=user_prompt)], functions=function_descriptions_multiple
)
print(first_response)
#print(str(first_response.additional_kwargs))
# Returns the function of the second request (book_flight)
# It takes all the arguments from the prompt but not the returned information
second_response = llm.predict_messages(
[
HumanMessage(content=user_prompt),
AIMessage(content=str(first_response.additional_kwargs)),
AIMessage(
role="function",
additional_kwargs={
"name": first_response.additional_kwargs["function_call"]["name"]
},
content=f"Completed function {first_response.additional_kwargs['function_call']['name']}",
),
],
functions=function_descriptions_multiple,
)
print(second_response)
# Returns the function of the third request (file_complaint)
third_response = llm.predict_messages(
[
HumanMessage(content=user_prompt),
AIMessage(content=str(first_response.additional_kwargs)),
AIMessage(content=str(second_response.additional_kwargs)),
AIMessage(
role="function",
additional_kwargs={
"name": second_response.additional_kwargs["function_call"]["name"]
},
content=f"Completed function {second_response.additional_kwargs['function_call']['name']}",
),
],
functions=function_descriptions_multiple,
)
print(third_response)
# Conversational reply at the end of requests
fourth_response = llm.predict_messages(
[
HumanMessage(content=user_prompt),
AIMessage(content=str(first_response.additional_kwargs)),
AIMessage(content=str(second_response.additional_kwargs)),
AIMessage(content=str(third_response.additional_kwargs)),
AIMessage(
role="function",
additional_kwargs={
"name": third_response.additional_kwargs["function_call"]["name"]
},
content=f"Completed function {third_response.additional_kwargs['function_call']['name']}",
),
],
functions=function_descriptions_multiple,
)
print(fourth_response)
# It automatically fills the arguments with correct info based on the prompt
# Note: the function does not exist yet
output = completion.choices[0].message
print(output)
return output, docs
# Example usage:
pdf_path = "./DummyUseCase.pdf"
db = create_vectordb_from_pdf(pdf_path)
user_prompt = """
You are a helpful assistant that that can answer questions about Application Production Support issues in Capital Markets
based on provided production incident data and relevant use cases.
Can you track order number GPQ-1234, get more descriptions on that order
Provide additional details by extracting logs from NAS using order number.
Also provide activity summary of that order
"""
query = "Can you tack order number GPQ1234?"
response, docs = get_response_from_query(db, user_prompt)
# print(textwrap.fill(response, width=200))