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

MPNet implementation #1

Merged
merged 3 commits into from
Feb 15, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -161,4 +161,5 @@ cython_debug/
#.idea/

gptstew/.env!/gptstew/resources/
.idea
.idea
.vscode
31 changes: 25 additions & 6 deletions index/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,29 @@
BIOFIND_DICT_SRC = "resources/dictionaries/pd/biofind.csv"
BIOFIND_EMBEDDINGS_SRC = "resources/embeddings/biofind.csv"

COLORS_AD = {'adni': '#d62728', 'aibl': '#ff7f0e', 'emif': '#8c564b', 'jadni': '#7f7f7f',
'a4': '#aec7e8', 'dod-adni': '#ffbb78', 'prevent-ad': '#98df8a', 'arwibo': '#ff9896',
'i-adni': '#c5b0d5', 'edsd': '#c49c94', 'pharmacog': '#c7c7c7',
'vita': '#bcbd22', 'abvib': '#e0d9e2', 'ad-mapper': '#800000'}
COLORS_AD = {
"adni": "#d62728",
"aibl": "#ff7f0e",
"emif": "#8c564b",
"jadni": "#7f7f7f",
"a4": "#aec7e8",
"dod-adni": "#ffbb78",
"prevent-ad": "#98df8a",
"arwibo": "#ff9896",
"i-adni": "#c5b0d5",
"edsd": "#c49c94",
"pharmacog": "#c7c7c7",
"vita": "#bcbd22",
"abvib": "#e0d9e2",
"ad-mapper": "#800000",
}

COLORS_PD = {'opdc': '#1f77b4', 'tpd': '#e377c2', 'biofind': '#9edae5', 'lrrk2': '#f7b6d2', 'luxpark': '#2ca02c',
'ppmi': '#9467bd', 'passionate': '#00ff00'}
COLORS_PD = {
"opdc": "#1f77b4",
"tpd": "#e377c2",
"biofind": "#9edae5",
"lrrk2": "#f7b6d2",
"luxpark": "#2ca02c",
"ppmi": "#9467bd",
"passionate": "#00ff00",
}
Empty file removed index/db/__init__.py
Empty file.
32 changes: 27 additions & 5 deletions index/embedding.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
from abc import ABC
import numpy as np
import openai
from sentence_transformers import SentenceTransformer


class EmbeddingModel(ABC):

def get_embedding(self, text: str) -> [float]:
pass

Expand All @@ -14,7 +14,6 @@ def get_embeddings(self, messages: [str]) -> [[float]]:


class GPT4Adapter(EmbeddingModel):

def __init__(self, api_key: str):
self.api_key = api_key
openai.api_key = api_key
Expand All @@ -28,19 +27,42 @@ def get_embedding(self, text: str, model="text-embedding-ada-002"):
return None
if isinstance(text, str):
text = text.replace("\n", " ")
return openai.Embedding.create(input=[text], model=model)['data'][0]['embedding']
return openai.Embedding.create(input=[text], model=model)["data"][0][
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not really a problem syntax wise but I am wondering about this formatting choice

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm using Black Formatter on VScode and it automatically formats on save. I will try to disable it.

"embedding"
]
except Exception as e:
logging.error(f"Error getting embedding for {text}: {e}")
return None

def get_embeddings(self, messages: [str], model="text-embedding-ada-002"):
# store index of nan entries
response = openai.Embedding.create(input=messages, model=model)
return [item['embedding'] for item in response['data']]
return [item["embedding"] for item in response["data"]]


class TextEmbedding:
class MPNetAdapter(EmbeddingModel):
def __init__(self):
logging.getLogger().setLevel(logging.INFO)

def get_embedding(self, text: str, model="sentence-transformers/all-mpnet-base-v2"):
mpnet_model = SentenceTransformer(model)
logging.info(f"Getting embedding for {text}")
try:
if text is None or text == "" or text is np.nan:
logging.warn(f"Empty text passed to get_embedding")
return None
if isinstance(text, str):
text = text.replace("\n", " ")
return mpnet_model.encode(text)
except Exception as e:
logging.error(f"Error getting embedding for {text}: {e}")
return None

def get_embeddings(self, messages: [str]) -> [[float]]:
return [self.get_embedding(msg) for msg in messages]


class TextEmbedding:
def __init__(self, text: str, embedding: [float]):
self.text = text
self.embedding = embedding
Loading
Loading