Skip to content

Commit

Permalink
Added a test module, renamed pinembed.py
Browse files Browse the repository at this point in the history
  • Loading branch information
Daethyra committed Oct 3, 2023
1 parent 5855e5d commit 1f7b2e6
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 3 deletions.
6 changes: 3 additions & 3 deletions Auto-Embedder/.env.template
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
[OpenAI]
OPENAI_API_KEY = sk-
OPENAI_API_KEY = sk-[...]
MODEL = text-embeddings-ada-002 # gpt-3.5-turbo # gpt-4 # gpt-4-32k
TEMPERATURE = 0

[Pinecone]
PINECONE_API_KEY =
PINECONE_ENVIRONMENT =
PINEDEX =
PINECONE_ENVIRONMENT = us-central1-gcp
PINEDEX = default_name
File renamed without changes.
71 changes: 71 additions & 0 deletions Auto-Embedder/test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import unittest
from unittest.mock import patch
import os
import logging
import asyncio
from dotenv import load_dotenv
from datetime import datetime
from typing import Dict, Union, List
import openai
import pinecone
import backoff

# Load environment variables from .env file
load_dotenv()

# Initialize logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

class EnvConfig:
"""Class for handling environment variables and API keys."""

def __init__(self) -> None:
"""Initialize environment variables."""
self.openai_key: str = os.getenv("OPENAI_API_KEY")
self.pinecone_key: str = os.getenv("PINECONE_API_KEY")
self.pinecone_environment: str = os.getenv("PINECONE_ENVIRONMENT")
self.pinecone_environment: str = os.getenv("PINEDEX")

class OpenAIHandler:
"""Class for handling OpenAI operations."""

def __init__(self, config: EnvConfig) -> None:
"""Initialize OpenAI API key."""
openai.api_key = config.openai_key

@backoff.on_exception(backoff.expo, Exception, max_tries=3)
async def create_embedding(self, input_text: str) -> Dict[str, Union[int, List[float]]]:
"""
Create an embedding using OpenAI.
Parameters:
input_text (str): The text to be embedded.
Returns:
Dict[str, Union[int, List[float]]]: The embedding response.
"""
response = openai.Embedding.create(
model="text-embedding-ada-002",engine="ada",
text=input_text,
)
return response

# Create test class
class TestOpenAIHandler(unittest.TestCase):
# Set up test environment
def setUp(self):
self.config = EnvConfig()
self.openai_handler = OpenAIHandler(self.config)

# Test create_embedding method
@patch('openai.Embedding.create')
def test_create_embedding(self, mock_create):
input_text = 'This is a test'
expected_response = {'id': 12345, 'embedding': [1.0, 2.0, 3.0]}
mock_create.return_value = expected_response
response = self.openai_handler.create_embedding(input_text)
self.assertEqual(response, expected_response)

if __name__ == "__main__":
unittest.main()

0 comments on commit 1f7b2e6

Please sign in to comment.