# Copyright 2023 OpenSPG Authors # # Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except # in compliance with the License. You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software distributed under the License # is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express # or implied. from typing import Union, Iterable from openai import OpenAI from kag.interface import VectorizeModelABC, EmbeddingVector @VectorizeModelABC.register("openai") class OpenAIVectorizeModel(VectorizeModelABC): """ A class that extends the VectorizeModelABC base class. It invokes OpenAI or OpenAI-compatible embedding services to convert texts into embedding vectors. """ def __init__( self, model: str = "text-embedding-3-small", api_key: str = "", base_url: str = "", vector_dimensions: int = None, timeout: float = None, ): """ Initializes the OpenAIVectorizeModel instance. Args: model (str, optional): The model to use for embedding. Defaults to "text-embedding-3-small". api_key (str, optional): The API key for accessing the OpenAI service. Defaults to "". base_url (str, optional): The base URL for the OpenAI service. Defaults to "". vector_dimensions (int, optional): The number of dimensions for the embedding vectors. Defaults to None. """ super().__init__(vector_dimensions) self.model = model self.timeout = timeout self.client = OpenAI(api_key=api_key, base_url=base_url) def vectorize( self, texts: Union[str, Iterable[str]] ) -> Union[EmbeddingVector, Iterable[EmbeddingVector]]: """ Vectorizes a text string into an embedding vector or multiple text strings into multiple embedding vectors. Args: texts (Union[str, Iterable[str]]): The text or texts to vectorize. Returns: Union[EmbeddingVector, Iterable[EmbeddingVector]]: The embedding vector(s) of the text(s). """ results = self.client.embeddings.create( input=texts, model=self.model, timeout=self.timeout ) results = [item.embedding for item in results.data] if isinstance(texts, str): assert len(results) == 1 return results[0] else: assert len(results) == len(texts) return results