Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Performance Metrics #38

Open
wants to merge 54 commits into
base: main
Choose a base branch
from
Open
Changes from 9 commits
Commits
Show all changes
54 commits
Select commit Hold shift + click to select a range
5a2e0be
adds timeit functionality to chat and query functions
jeisenman23 Nov 14, 2024
86e2a51
reformatting time metrics
jeisenman23 Nov 14, 2024
93a7d97
trying to fix lint
jeisenman23 Nov 15, 2024
4e4dc4b
fixing lint
jeisenman23 Nov 15, 2024
fab34d9
reducing complexity
jeisenman23 Nov 15, 2024
785ec4d
removing white spaces
jeisenman23 Nov 15, 2024
b0429a4
fixing lint
jeisenman23 Nov 15, 2024
a41b030
fixing return statement
jeisenman23 Nov 15, 2024
dba79f8
fixing docs
jeisenman23 Nov 15, 2024
dc4579a
removing timeit as optional argument
jeisenman23 Nov 21, 2024
bfdd13f
changing performance metrics according to stream mode:
jeisenman23 Nov 21, 2024
96be266
update test
jeisenman23 Nov 21, 2024
6d37186
change test to fit performance metrics
jeisenman23 Dec 10, 2024
4aa6fb2
change test to fit performance metrics
jeisenman23 Dec 10, 2024
0ac1ca9
adding back query into chat
jeisenman23 Dec 10, 2024
f8c3d1a
removing await
jeisenman23 Dec 10, 2024
4018c59
fixing test
jeisenman23 Dec 20, 2024
4ca2970
fixing test
jeisenman23 Dec 20, 2024
34c58c0
removing whitespace
jeisenman23 Dec 20, 2024
abd7000
fixing space
jeisenman23 Dec 20, 2024
d94403c
finicky flake8 error fix
jeisenman23 Dec 20, 2024
63070ef
fixing elm tests
jeisenman23 Dec 20, 2024
20fb602
ensuring test cases
jeisenman23 Dec 20, 2024
15c417a
reversing - statement
jeisenman23 Dec 20, 2024
0186fdf
removing whitespace
jeisenman23 Dec 20, 2024
d4a9bf0
removing whitespace
jeisenman23 Dec 20, 2024
fc88d7a
fixing line issue
jeisenman23 Dec 27, 2024
878e4f0
fixing osti bug
jeisenman23 Jan 2, 2025
b496352
adding spaces for engineer query
jeisenman23 Jan 2, 2025
991d3e3
adding spaces for chat function
jeisenman23 Jan 2, 2025
af86612
remove trailing whitespaces
jeisenman23 Jan 2, 2025
ae37420
Merge branch 'main' into time
jeisenman23 Jan 2, 2025
3232c02
fixing OSTI bug
jeisenman23 Jan 6, 2025
5334535
removing comments for flake
jeisenman23 Jan 6, 2025
8f4cdcc
making line shorter
jeisenman23 Jan 6, 2025
8d15faa
line too long
jeisenman23 Jan 6, 2025
8348af3
adding blank line
jeisenman23 Jan 6, 2025
667fe05
rerun of actions
jeisenman23 Jan 6, 2025
80be899
changing first
jeisenman23 Jan 6, 2025
564f5d4
attempting to fix osti
jeisenman23 Jan 6, 2025
bc196f1
attempt to fix OSTI in multiple envs
jeisenman23 Jan 6, 2025
022f734
removing test and fixing test
jeisenman23 Jan 6, 2025
371dcc2
inputting local change that works
jeisenman23 Jan 6, 2025
6549134
fixing lint
jeisenman23 Jan 6, 2025
ab7449c
debug statement
jeisenman23 Jan 6, 2025
96c3b87
attempt to fix escape sequence
jeisenman23 Jan 6, 2025
24b41c9
attempting to fix str
jeisenman23 Jan 6, 2025
7f55760
fixing escape
jeisenman23 Jan 6, 2025
4eac5dc
getting get pages to work
jeisenman23 Jan 7, 2025
2ed61b9
clean code
jeisenman23 Jan 7, 2025
7819a13
fixing linter
jeisenman23 Jan 7, 2025
80c92b6
fixing linter
jeisenman23 Jan 7, 2025
0e6b9f9
fixing linter
jeisenman23 Jan 7, 2025
18a4465
fixing over indent
jeisenman23 Jan 7, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 36 additions & 16 deletions elm/wizard.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
ELM energy wizard
"""
from abc import ABC, abstractmethod
from time import perf_counter
import copy
import os
import json
Expand Down Expand Up @@ -61,10 +62,12 @@ def query_vector_db(self, query, limit=100):
ranked strings/scores outputs.
"""

def engineer_query(self, query, token_budget=None, new_info_threshold=0.7,
convo=False):
def engineer_query(self, query,
token_budget=None,
new_info_threshold=0.7,
convo=False,
timeit=False):
grantbuster marked this conversation as resolved.
Show resolved Hide resolved
"""Engineer a query for GPT using the corpus of information

Parameters
----------
query : str
Expand All @@ -87,6 +90,9 @@ def engineer_query(self, query, token_budget=None, new_info_threshold=0.7,
references : list
The list of references (strs) used in the engineered prompt is
returned here
vector_query_time : float
grantbuster marked this conversation as resolved.
Show resolved Hide resolved
if timeit is True, then we will time how long the query to the
vectorDB takes
"""

self.messages.append({"role": "user", "content": query})
Expand All @@ -99,9 +105,10 @@ def engineer_query(self, query, token_budget=None, new_info_threshold=0.7,
query = '\n\n'.join(query)

token_budget = token_budget or self.token_budget

start_time = perf_counter()
strings, _, idx = self.query_vector_db(query)

end_time = perf_counter()
vector_query_time = end_time - start_time
message = copy.deepcopy(self.MODEL_INSTRUCTION)
question = f"\n\nQuestion: {query}"
used_index = []
Expand All @@ -125,7 +132,8 @@ def engineer_query(self, query, token_budget=None, new_info_threshold=0.7,
message = message + question
used_index = np.array(used_index)
references = self.make_ref_list(used_index)

if timeit:
return message, references, used_index, vector_query_time
return message, references, used_index

@abstractmethod
Expand All @@ -152,10 +160,10 @@ def chat(self, query,
token_budget=None,
new_info_threshold=0.7,
print_references=False,
return_chat_obj=False):
return_chat_obj=False,
timeit=False):
grantbuster marked this conversation as resolved.
Show resolved Hide resolved
"""Answers a query by doing a semantic search of relevant text with
embeddings and then sending engineered query to the LLM.

Parameters
----------
query : str
Expand Down Expand Up @@ -184,7 +192,8 @@ def chat(self, query,
valid ref_col.
return_chat_obj : bool
Flag to only return the ChatCompletion from OpenAI API.

timeit : bool
Flag to return the performance metrics on API calls.
Returns
grantbuster marked this conversation as resolved.
Show resolved Hide resolved
-------
response : str
Expand All @@ -195,12 +204,15 @@ def chat(self, query,
references : list
If debug is True, the list of references (strs) used in the
engineered prompt is returned here
performance : dict
If timeit is True, returns dictionary with keys of total_chat_time,
chat_completion_time and vectordb_query_time.
"""

start_chat_time = perf_counter()
out = self.engineer_query(query, token_budget=token_budget,
new_info_threshold=new_info_threshold,
convo=convo)
query, references, _ = out
convo=convo, timeit=True)
query, references, _, vector_query_time = out

messages = [{"role": "system", "content": self.MODEL_ROLE},
{"role": "user", "content": query}]
Expand All @@ -209,18 +221,18 @@ def chat(self, query,
messages=messages,
temperature=temperature,
stream=stream)
start_completion_time = perf_counter()

response = self._client.chat.completions.create(**kwargs)

finish_completion_time = perf_counter()
chat_completion_time = start_completion_time - finish_completion_time
if return_chat_obj:
return response, query, references

if stream:
for chunk in response:
chunk_msg = chunk.choices[0].delta.content or ""
response_message += chunk_msg
print(chunk_msg, end='')

else:
response_message = response.choices[0].message.content

Expand All @@ -234,7 +246,15 @@ def chat(self, query,
response_message += ref_msg
if stream:
print(ref_msg)

end_time = perf_counter()
total_chat_time = start_chat_time - end_time
performance = {
"total_chat_time": total_chat_time,
"chat_completion_time": chat_completion_time,
grantbuster marked this conversation as resolved.
Show resolved Hide resolved
"vectordb_query_time": vector_query_time
}
if timeit and debug:
return response_message, query, references, performance
if debug:
return response_message, query, references
else:
Expand Down
Loading