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 OpenAI API Call #71 #1

Merged
merged 1 commit into from
Mar 1, 2024
Merged
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
8 changes: 4 additions & 4 deletions src/semantra/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from abc import ABC, abstractmethod

import numpy as np
import openai
from openai import OpenAI
import tiktoken
import torch
from dotenv import load_dotenv
Expand Down Expand Up @@ -114,7 +114,7 @@ def __init__(
"OpenAI API key not set. Please set the OPENAI_API_KEY environment variable or create a `.env` file with the key in the current working directory or the Semantra directory, which is revealed by running `semantra --show-semantra-dir`."
)

openai.api_key = os.getenv("OPENAI_API_KEY")
self.client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))

self.model_name = model_name
self.num_dimensions = num_dimensions
Expand All @@ -141,8 +141,8 @@ def get_text_chunks(self, _: str, tokens) -> "list[str]":

def embed(self, tokens, offsets, _is_query=False) -> "list[list[float]]":
texts = [tokens[i:j] for i, j in offsets]
response = openai.Embedding.create(model=self.model_name, input=texts)
return np.array([data["embedding"] for data in response["data"]])
response = self.client.embeddings.create(model=self.model_name, input=texts)
return np.array([embedding.embedding for embedding in response.data])


def zero_if_none(x):
Expand Down