Skip to content

Commit

Permalink
Merge pull request #274 from ChatWithPDF/restructure
Browse files Browse the repository at this point in the history
changes to 4
  • Loading branch information
Gautam-Rajeev authored Nov 9, 2023
2 parents 6667d8a + dc38d3a commit 1b58a99
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 80 deletions.
54 changes: 31 additions & 23 deletions config.json
Original file line number Diff line number Diff line change
@@ -1,19 +1,27 @@
{
"models": [
{
"serviceName": "convo_starter",
"modelBasePath": "src/text_classification/convo_starter_orgbot/local/.",
"apiBasePath": "text_classification/convo_starter_orgbot/local/",
"containerPort": 8000,
"environment": { },
"nginx": [],
"build": false
},
{
"serviceName": "search_tf",
"modelBasePath": "src/search/tf_search/local/.",
"apiBasePath": "search/tf_search/local/",
"containerPort": 8000,
"environment": { },
"nginx": ["client_max_body_size 100M;", "proxy_read_timeout 600;", "proxy_connect_timeout 600;", "proxy_send_timeout 600;"],
"build": true
"build": false
},

{
"serviceName": "embedding_instructor_gpu",
"modelBasePath": "src/embeddings/instructor_gpu/local/.",
"apiBasePath": "embeddings/instructor_gpu/local/",
"serviceName": "embedding_instructor",
"modelBasePath": "src/embeddings/instructor/local/.",
"apiBasePath": "/embeddings/instructor/local",
"containerPort": 8000,
"environment": {
"OPENAI_API_KEY": "${OPENAI_API_KEY}",
Expand All @@ -23,6 +31,17 @@
"constraints": ["node.labels.node_vm_type==gpu"],
"build": false
},
{
"serviceName": "embedding_openai",
"modelBasePath": "src/embeddings/openai/remote/.",
"apiBasePath": "/embeddings/openai/remote",
"containerPort": 8000,
"environment": {
"OPENAI_API_KEY": "${OPENAI_API_KEY}"
},
"nginx": ["client_max_body_size 100M;", "proxy_read_timeout 600;", "proxy_connect_timeout 600;", "proxy_send_timeout 600;"],
"build": false
},
{
"serviceName": "asr_whisper_en",
"modelBasePath": "src/asr/whisper_en/local/.",
Expand Down Expand Up @@ -72,16 +91,6 @@
"nginx": [],
"build": false
},
{
"serviceName": "flow_classification",
"modelBasePath": "src/text_classification/flow_classification/local/.",
"apiBasePath": "text_classification/flow_classification/local/",
"containerPort": 8000,
"environment": {},
"nginx": [],
"constraints": ["node.role==worker"],
"build": false
},
{
"serviceName": "text_translation_azure_dict",
"modelBasePath": "src/text_translation/azure_dict/remote/.",
Expand Down Expand Up @@ -178,27 +187,26 @@
"build": false
},
{
"serviceName": "embedding_instructor",
"modelBasePath": "src/embeddings/instructor/local/.",
"apiBasePath": "/embeddings/instructor/local",
"serviceName": "llm_openai_gpt3",
"modelBasePath": "src/llm/openai/chatgpt3/.",
"apiBasePath": "/llm/openai/chatgpt3",
"containerPort": 8000,
"environment": {
"OPENAI_API_KEY": "${OPENAI_API_KEY}"
},
"nginx": ["client_max_body_size 100M;", "proxy_read_timeout 600;", "proxy_connect_timeout 600;", "proxy_send_timeout 600;"],
"nginx": [],
"constraints": ["node.labels.node_vm_type==gpu"],
"build": false
},
{
"serviceName": "llm_openai_gpt3",
"modelBasePath": "src/llm/openai/chatgpt3/.",
"apiBasePath": "/llm/openai/chatgpt3",
"serviceName": "llm_openai_gpt4",
"modelBasePath": "src/llm/openai/chatgpt4/.",
"apiBasePath": "/llm/openai/chatgpt4",
"containerPort": 8000,
"environment": {
"OPENAI_API_KEY": "${OPENAI_API_KEY}"
},
"nginx": [],
"constraints": ["node.labels.node_vm_type==gpu"],
"build": true
}
]
Expand Down
54 changes: 15 additions & 39 deletions src/embeddings/openai/remote/model.py
Original file line number Diff line number Diff line change
@@ -1,52 +1,28 @@
import os
import openai
from openai.embeddings_utils import get_embedding
from cache import AsyncTTL
from request import ModelRequest
import numpy as np
import pandas as pd
import tiktoken
import ast
from sklearn.metrics.pairwise import cosine_similarity

openai.api_key = os.getenv("OPENAI_API_KEY")

from openai import OpenAI
import os

class Model:
embedding_df = None
embedding_model = "text-embedding-ada-002"
embedding_encoding = "cl100k_base" # this the encoding for text-embedding-ada-002
max_tokens = 8000 # the maximum for text-embedding-ada-002 is 8191

def __new__(cls, context):
cls.context = context
if not hasattr(cls, 'instance'):
cls.embedding_df = pd.read_csv('src/embeddings/openai/remote/akai.csv')
cls.embedding_df['embedding'] = cls.embedding_df['embedding'].apply(ast.literal_eval)
cls.client = OpenAI(
api_key=os.getenv("OPENAI_API_KEY"),
)
cls.instance = super(Model, cls).__new__(cls)
return cls.instance

@AsyncTTL(time_to_live=600000, maxsize=1024)
async def inference(self, request: ModelRequest):
print("request.prompt", request.prompt)
new_prompt_embedding = get_embedding(request.prompt, engine=self.embedding_model)
similarity_scores = cosine_similarity(
[new_prompt_embedding], np.stack(self.embedding_df['embedding'], axis=0))[0]
most_similar_indices = np.argsort(similarity_scores)[::-1]
most_similar_prompts = self.embedding_df.loc[most_similar_indices, ['combined_prompt', 'combined_content']]
most_similar_prompts['similarity_score'] = np.sort(similarity_scores)[::-1]
similar_content = most_similar_prompts.iloc[0:20]
sim_cutoff_range = np.max(similar_content['similarity_score']) - request.similarity_score_range
similar_content_df = similar_content.loc[similar_content['similarity_score'] >= sim_cutoff_range, :]
similar_content_df1 = similar_content_df.drop(columns='similarity_score')
similar_content_dict = similar_content_df1.to_dict('records')
# modified_content_dict = remove_content_tags_from_dic(similar_content_dict)
print("similar_content_dict", similar_content_dict)
return (similar_content_dict)
# Modify this function according to model requirements such that inputs and output remains the same
query = request.query

async def create_embeddings(self, embedding_df):
encoding = tiktoken.get_encoding(self.embedding_encoding)
embedding_df["n_tokens"] = embedding_df.combined_prompt.apply(lambda x: len(encoding.encode(x)))
embedding_df["embedding"] = embedding_df.combined_prompt.apply(
lambda x: get_embedding(x, engine=self.embedding_model))
return embedding_df
if(query != None):
embedding = self.client.embeddings.create(
input=query,
model=self.embedding_model,
).data[0].embedding
return [embedding]

return "Invalid input"
9 changes: 5 additions & 4 deletions src/embeddings/openai/remote/request.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import json


class ModelRequest():
def __init__(self, prompt, similarity_score_range=0):
self.prompt = prompt
self.similarity_score_range = similarity_score_range
def __init__(self, query=None, df = None, query_type = None):
# Url to download csv file
self.query = query # String
self.query_type = query_type
self.df = df

def to_json(self):
return json.dumps(self, default=lambda o: o.__dict__,
Expand Down
6 changes: 1 addition & 5 deletions src/embeddings/openai/remote/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
aiohttp==3.8.4
quart==0.18.3
quart
async-cache==1.1.1
requests
openai
numpy
pandas
tiktoken
sklearn
9 changes: 2 additions & 7 deletions src/llm/openai/chatgpt4/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ def __new__(cls, context):
cls.instance = super(Model, cls).__new__(cls)
return cls.instance

@AsyncTTL(time_to_live=600000, maxsize=1024)
@retry(wait=wait_random_exponential(min=1, max=20), stop=stop_after_attempt(6))
async def inference(self, request: ModelRequest):
response = await openai_async.chat_complete(
Expand All @@ -24,11 +23,7 @@ async def inference(self, request: ModelRequest):
payload={
"model": "gpt-4",
"temperature": 0,
"messages": [{"role":"user","content" : request.prompt}],
"messages": request.prompt,
},
)
try:
ans = response.json()["choices"][0]["message"]["content"]
return {"ans":ans}
except:
return response.json()
return response.json()
4 changes: 2 additions & 2 deletions src/llm/openai/chatgpt4/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
aiohttp==3.8.4
quart==0.18.3
quart
async-cache==1.1.1
requests
openai
openai_async
tenacity
tenacity

0 comments on commit 1b58a99

Please sign in to comment.