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

Update to make it compatible with new OpenAI SDK #63

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
20 changes: 19 additions & 1 deletion src/ai.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ def embeddings(texts, **kw):
model = kw.get('model','text-embedding-ada-002')
llm = openai.model(model)
resp = llm.embed_many(texts, **kw)
# Convert usage object to dictionary if needed
if hasattr(resp['usage'], 'prompt_tokens'):
resp['usage'] = {
'prompt_tokens': resp['usage'].prompt_tokens,
'completion_tokens': resp['usage'].completion_tokens,
'total_tokens': resp['usage'].total_tokens
}
resp['model'] = model
return resp

Expand All @@ -42,7 +49,17 @@ def get_token_count(text):

def stats_callback(out, resp, self):
model = self.config['model']
usage = resp['usage']
# Handle both dictionary-style and attribute-style responses
usage = resp.usage if hasattr(resp, 'usage') else resp['usage']

# Convert usage to dictionary if it's an object
if hasattr(usage, 'prompt_tokens'):
usage = {
'prompt_tokens': usage.prompt_tokens,
'total_tokens': usage.total_tokens,
'completion_tokens': getattr(usage, 'completion_tokens', 0)
}

usage['call_cnt'] = 1
if 'text' in out:
usage['completion_chars'] = len(out['text'])
Expand All @@ -53,6 +70,7 @@ def stats_callback(out, resp, self):
if 'rtt' in out:
usage['rtt'] = out['rtt']
usage['rtt_cnt'] = 1

usage_stats.incr(f'usage:v4:[date]:[user]', {f'{k}:{model}':v for k,v in usage.items()})
usage_stats.incr(f'hourly:v4:[date]', {f'{k}:{model}:[hour]':v for k,v in usage.items()})
#print('STATS_CALLBACK', usage, flush=True) # XXX
Expand Down
10 changes: 8 additions & 2 deletions src/gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,13 @@ def b_ask():
n_after=n_after,
model=ss['model'],
)
usage = resp.get('usage',{})
usage = {}
if hasattr(resp.get('usage'), 'prompt_tokens'):
usage = {
'prompt_tokens': resp['usage'].prompt_tokens,
'completion_tokens': resp['usage'].completion_tokens,
'total_tokens': resp['usage'].total_tokens
}
usage['cnt'] = 1
ss['debug']['model.query.resp'] = resp
ss['debug']['resp.usage'] = usage
Expand All @@ -258,7 +264,7 @@ def b_ask():
a = resp['text'].strip()
ss['answer'] = a
output_add(q,a)
st.experimental_rerun() # to enable the feedback buttons
st.rerun()

def b_clear():
if st.button('clear output'):
Expand Down
10 changes: 7 additions & 3 deletions src/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,16 @@ def get_vectors(text_list):
usage = Counter()
for i,texts in enumerate(batch(text_list, batch_size)):
resp = ai.embeddings(texts)
v = resp['vectors']
u = resp['usage']
v = resp.get('vectors', getattr(resp, 'vectors', []))
u = resp.get('usage', getattr(resp, 'usage', {}))
u['call_cnt'] = 1
usage.update(u)
vectors.extend(v)
return {'vectors':vectors, 'usage':dict(usage), 'model':resp['model']}
return {
'vectors': vectors,
'usage': dict(usage),
'model': getattr(resp, 'model', resp.get('model', ''))
}

def index_file(f, filename, fix_text=False, frag_size=0, cache=None):
"return vector index (dictionary) for a given PDF file"
Expand Down
Empty file modified src/run.sh
100644 → 100755
Empty file.