This repository has been archived on 2025-11-27. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
Fitz_Studio/src/ConexionApis/OpenAi_conexion.py
T
egutierrez 613cd90662 Add OpenAI and PostgreSQL credential management
- Implemented OpenAICredencial class for managing OpenAI API keys.
- Created OpenAICredencialModel and OpenAICredencialMapper for SQLAlchemy integration.
- Developed OpenAICredencialRepo for CRUD operations on OpenAI credentials.
- Established OpenAICliente class for interacting with OpenAI API.
- Introduced PostgresCredencial class for managing PostgreSQL connection details.
- Created PostgresCredencialModel and PostgresCredencialMapper for SQLAlchemy integration.
- Developed PostgresCredencialRepo for CRUD operations on PostgreSQL credentials.
- Added base connection class and PostgreSQL connection implementation.
- Included environment variable loading for sensitive data management.
2025-05-05 23:54:17 +02:00

59 lines
2.5 KiB
Python

from openai import OpenAI
from src.ApiKeys.openai_apikey import OpenAICredencial
class OpenAICliente:
def __init__(self, credencial: OpenAICredencial):
"""
Inicializa el cliente de OpenAI con una instancia de OpenAICredencial.
"""
self.credencial = credencial
self.client = OpenAI(api_key=self.credencial.api_key)
if self.credencial.organizacion:
self.client.organization = self.credencial.organizacion
# --- Chat Completions ---
def chat_completion(self, model: str, messages: list, **kwargs):
return self.client.chat.completions.create(model=model, messages=messages, **kwargs)
# --- Text Completions (legacy) ---
def completion(self, model: str, prompt: str, **kwargs):
return self.client.completions.create(model=model, prompt=prompt, **kwargs)
# --- Text Edits ---
def edit(self, model: str, input: str, instruction: str, **kwargs):
return self.client.edits.create(model=model, input=input, instruction=instruction, **kwargs)
# --- Embeddings ---
def embedding(self, model: str, input: str | list[str], **kwargs):
return self.client.embeddings.create(model=model, input=input, **kwargs)
# --- Moderation ---
def moderation(self, input: str | list[str], model="text-moderation-latest", **kwargs):
return self.client.moderations.create(input=input, model=model, **kwargs)
# --- Image Generation ---
def image_generate(self, prompt: str, **kwargs):
return self.client.images.generate(prompt=prompt, **kwargs)
# --- Audio Transcription ---
def audio_transcribe(self, model: str, file_path: str, **kwargs):
with open(file_path, "rb") as f:
return self.client.audio.transcriptions.create(model=model, file=f, **kwargs)
# --- Audio Translation ---
def audio_translate(self, model: str, file_path: str, **kwargs):
with open(file_path, "rb") as f:
return self.client.audio.translations.create(model=model, file=f, **kwargs)
# --- File Upload ---
def file_upload(self, file_path: str, purpose="fine-tune", **kwargs):
with open(file_path, "rb") as f:
return self.client.files.create(file=f, purpose=purpose, **kwargs)
# --- File List ---
def file_list(self, **kwargs):
return self.client.files.list(**kwargs)
# --- File Delete ---
def file_delete(self, file_id: str, **kwargs):
return self.client.files.delete(file_id=file_id, **kwargs)