-
Notifications
You must be signed in to change notification settings - Fork 112
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #274 from ChatWithPDF/restructure
changes to 4
- Loading branch information
Showing
6 changed files
with
56 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |