Created
December 18, 2023 02:12
-
-
Save hathibelagal-dev/eaab1c1a54eff279ff64206bbb7d0855 to your computer and use it in GitHub Desktop.
Sentiment Analysis Using Google's Text Embeddings.
This file contains hidden or 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
import vertexai.language_models as v | |
import jax.numpy as jnp | |
import jax.numpy.linalg as jl | |
model = v.TextEmbeddingModel.from_pretrained( | |
"textembedding-gecko" | |
) | |
sentiment_checks = [ | |
"This is very good. I loved it! Thanks", | |
"This was bad. I hated it! Yuck" | |
] | |
sce = model.get_embeddings(sentiment_checks) | |
prompts = [ | |
"Wow! Cool", | |
"Dumbest thing ever! I have nightmares now.", | |
"You are dumb!" | |
] | |
positive = jnp.array(sce[0].values) | |
negative = jnp.array(sce[1].values) | |
prompt_embeddings = model.get_embeddings(prompts) | |
def similarity(x, y): | |
return x @ y.T / (jl.norm(x) * jl.norm(y)) | |
for i, prompt in enumerate(prompt_embeddings): | |
prompt = jnp.array(prompt.values) | |
v1 = similarity(prompt, positive) | |
v2 = similarity(prompt, negative) | |
print(prompts[i]) | |
print("Positive" if v1 > v2 else "Negative") | |
print("---") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment