KingZack commited on
Commit
3978095
·
1 Parent(s): fddb754

completed VectorizationService to get embeddings

Browse files
src/ctp_slack_bot/services/VectorizationService.py CHANGED
@@ -2,18 +2,18 @@ from pydantic import BaseModel, validator
2
  from typing import List, Optional
3
  from ctp_slack_bot.core.config import settings
4
  import numpy as np
5
- from openai import AsyncOpenAI # Updated import
6
 
7
 
8
- class VectorizationService(BaseModel):
9
  """
10
  Service for vectorizing chunks of text data.
11
  """
12
  def __init__(self):
13
- self.client = AsyncOpenAI(api_key=settings.OPENAI_API_KEY)
14
 
15
 
16
- async def get_embeddings(self, texts: List[str]) -> np.ndarray:
17
  """Get embeddings for a list of texts using OpenAI's API.
18
 
19
  Args:
@@ -27,7 +27,7 @@ class VectorizationService(BaseModel):
27
  """
28
  try:
29
  # Use the initialized client instead of the global openai module
30
- response = await self.client.embeddings.create(
31
  model=settings.EMBEDDING_MODEL,
32
  input=texts,
33
  encoding_format="float" # Ensure we get raw float values
@@ -47,18 +47,13 @@ class VectorizationService(BaseModel):
47
 
48
  except Exception as e:
49
  print(f"Error getting embeddings: {str(e)}")
50
- raise
51
 
52
- def _test(self):
53
  """
54
  Test the vectorization service.
55
  """
56
-
57
- pass
58
-
59
-
60
-
61
- vs = VectorizationService()
62
- vs._test()
63
-
64
-
 
2
  from typing import List, Optional
3
  from ctp_slack_bot.core.config import settings
4
  import numpy as np
5
+ from openai import OpenAI
6
 
7
 
8
+ class VectorizationService():
9
  """
10
  Service for vectorizing chunks of text data.
11
  """
12
  def __init__(self):
13
+ self.client = OpenAI(api_key=settings.OPENAI_API_KEY)
14
 
15
 
16
+ def get_embeddings(self, texts: List[str]) -> np.ndarray:
17
  """Get embeddings for a list of texts using OpenAI's API.
18
 
19
  Args:
 
27
  """
28
  try:
29
  # Use the initialized client instead of the global openai module
30
+ response = self.client.embeddings.create(
31
  model=settings.EMBEDDING_MODEL,
32
  input=texts,
33
  encoding_format="float" # Ensure we get raw float values
 
47
 
48
  except Exception as e:
49
  print(f"Error getting embeddings: {str(e)}")
50
+ pass
51
 
52
+ def _test(self, list_of_strings: List[str] = ['Hello my sweet Svetlana.', 'You mean the world to me.']):
53
  """
54
  Test the vectorization service.
55
  """
56
+ print('embedding list', list_of_strings)
57
+ embeddings = self.get_embeddings(list_of_strings)
58
+ print(embeddings)
59
+ return embeddings