Skip to content

Commit

Permalink
feat: add info about job processor (#88)
Browse files Browse the repository at this point in the history
  • Loading branch information
quitrk authored Jun 17, 2024
1 parent cf28132 commit 84005f4
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 18 deletions.
47 changes: 30 additions & 17 deletions skynet/modules/ttt/summaries/jobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

from .persistence import db
from .processor import process, process_azure, process_open_ai
from .v1.models import DocumentMetadata, DocumentPayload, Job, JobId, JobStatus, JobType
from .v1.models import DocumentMetadata, DocumentPayload, Job, JobId, JobStatus, JobType, Processors

log = get_logger(__name__)

Expand All @@ -36,6 +36,20 @@ def can_run_next_job() -> bool:
return 'summaries:executor' in modules and (current_task is None or current_task.done())


def get_job_processor(customer_id: str) -> str:
options = get_credentials(customer_id)
secret = options.get('secret')
api_type = options.get('type')

if secret:
if api_type == CredentialsType.OPENAI.value:
return Processors.OPENAI
elif api_type == CredentialsType.AZURE_OPENAI.value:
return Processors.AZURE

return Processors.LOCAL


async def update_summary_queue_metric() -> None:
"""Update the queue size metric."""

Expand Down Expand Up @@ -69,8 +83,9 @@ async def create_job(job_type: JobType, payload: DocumentPayload, metadata: Docu

job_id = str(uuid.uuid4())

job = Job(id=job_id, payload=payload, type=job_type, metadata=metadata)

job = Job(
id=job_id, payload=payload, type=job_type, metadata=metadata, processor=get_job_processor(metadata.customer_id)
)
await db.set(job_id, Job.model_dump_json(job))

log.info(f"Created job {job.id}.")
Expand Down Expand Up @@ -128,23 +143,21 @@ async def run_job(job: Job) -> None:
customer_id = job.metadata.customer_id
options = get_credentials(customer_id)
secret = options.get('secret')
api_type = options.get('type')

if secret:
if api_type == CredentialsType.OPENAI.value:
log.info(f"Forwarding inference to OpenAI for customer {customer_id}")
processor = get_job_processor(customer_id) # may have changed since job was created

# needed for backwards compatibility
model = options.get('model') or options.get('metadata').get('model')
result = await process_open_ai(job.payload, job.type, secret, model)
if processor == Processors.OPENAI:
log.info(f"Forwarding inference to OpenAI for customer {customer_id}")

elif api_type == CredentialsType.AZURE_OPENAI.value:
log.info(f"Forwarding inference to Azure openai for customer {customer_id}")
# needed for backwards compatibility
model = options.get('model') or options.get('metadata').get('model')
result = await process_open_ai(job.payload, job.type, secret, model)
elif processor == Processors.AZURE:
log.info(f"Forwarding inference to Azure openai for customer {customer_id}")

metadata = options.get('metadata')
result = await process_azure(
job.payload, job.type, secret, metadata.get('endpoint'), metadata.get('deploymentName')
)
metadata = options.get('metadata')
result = await process_azure(
job.payload, job.type, secret, metadata.get('endpoint'), metadata.get('deploymentName')
)
else:
if customer_id:
log.info(f'Customer {customer_id} has no API key configured, falling back to local processing')
Expand Down
9 changes: 8 additions & 1 deletion skynet/modules/ttt/summaries/v1/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ class JobType(Enum):
SUMMARY = 'summary'


class Processors(Enum):
OPENAI = 'OPENAI'
AZURE = 'AZURE'
LOCAL = 'LOCAL'


class JobStatus(Enum):
PENDING = 'pending'
RUNNING = 'running'
Expand All @@ -34,11 +40,12 @@ class JobStatus(Enum):

# job model to expose to the API
class BaseJob(BaseModel):
duration: float = 0.0
id: str
processor: Processors = Processors.LOCAL
result: str | None = None
status: JobStatus = JobStatus.PENDING
type: JobType
duration: float = 0.0


# since private fields are not serialized, use a different model with required internals
Expand Down

0 comments on commit 84005f4

Please sign in to comment.