refactor: update llm.call to async await for improved performance #236
@@ -1,9 +1,9 @@
|
||||
from openai import OpenAI, APIConnectionError
|
||||
from openai import APIConnectionError
|
||||
from openai import AsyncOpenAI
|
||||
from .. import config
|
||||
from typing import Optional, Dict, Any
|
||||
import asyncio
|
||||
def call(
|
||||
async def call(
|
||||
system_prompt: str,
|
||||
user_prompt: str,
|
||||
model: str = None,
|
||||
@@ -16,18 +16,18 @@ def call(
|
||||
|
||||
if model is None:
|
||||
model = config.settings.llm_model
|
||||
|
||||
|
||||
response_format = None
|
||||
if json_schema is not None:
|
||||
response_format = _get_response_format(schema = json_schema,schema_name=schema_name)
|
||||
|
||||
client = OpenAI(
|
||||
|
||||
client = AsyncOpenAI(
|
||||
base_url=config.settings.llm_api_url,
|
||||
api_key=config.settings.llm_api_key,
|
||||
timeout=timeout,
|
||||
)
|
||||
try:
|
||||
response = client.chat.completions.create(
|
||||
response = await client.chat.completions.create(
|
||||
model=model,
|
||||
messages=[
|
||||
{"role": "system", "content": system_prompt},
|
||||
|
||||
@@ -82,7 +82,7 @@ async def extract_keywords(query: str, mode: str, limit: int) -> ExtractedKeywor
|
||||
|
||||
user_prompt += f"""Input:{text}"""
|
||||
|
||||
keywords = llm.call(
|
||||
keywords = await llm.call(
|
||||
system_prompt=system_prompt,
|
||||
user_prompt=user_prompt,
|
||||
json_schema = ExtractedKeywordsLLM.model_json_schema(),
|
||||
|
||||
@@ -292,7 +292,7 @@ async def _get_article_keywords(
|
||||
if not text:
|
||||
return []
|
||||
|
||||
raw = llm.call(
|
||||
raw = await llm.call(
|
||||
system_prompt=KEYWORD_SYSTEM_PROMPT,
|
||||
user_prompt=f"Artikel:\n{text}",
|
||||
temperature=0.3,
|
||||
|
||||
@@ -444,7 +444,7 @@ async def generate_person_bio(
|
||||
|
||||
user_prompt = f"Daten zur Person:\n{person_json}"
|
||||
|
||||
bio = llm.call(
|
||||
bio = await llm.call(
|
||||
system_prompt=system_prompt,
|
||||
user_prompt=user_prompt,
|
||||
temperature=0.8,
|
||||
|
||||
Reference in New Issue
Block a user