nahj get metadata
This commit is contained in:
parent
447d37a274
commit
31da2788a3
|
|
@ -11,6 +11,11 @@ import asyncio
|
||||||
import traceback
|
import traceback
|
||||||
from openai import AsyncOpenAI
|
from openai import AsyncOpenAI
|
||||||
|
|
||||||
|
import copy, asyncio, traceback
|
||||||
|
from openai import OpenAI, AsyncOpenAI, LengthFinishReasonError
|
||||||
|
from typing import List, Union
|
||||||
|
from pydantic import BaseModel
|
||||||
|
|
||||||
today = f'{datetime.datetime.now().year}{datetime.datetime.now().month}{datetime.datetime.now().day}'
|
today = f'{datetime.datetime.now().year}{datetime.datetime.now().month}{datetime.datetime.now().day}'
|
||||||
|
|
||||||
SYSTEM_PROMPT = """
|
SYSTEM_PROMPT = """
|
||||||
|
|
@ -148,6 +153,99 @@ async def single_simple_async_proccess_item(
|
||||||
traceback.print_exc()
|
traceback.print_exc()
|
||||||
raise RuntimeError(f"⚠️ Error in API call: {str(e)}")
|
raise RuntimeError(f"⚠️ Error in API call: {str(e)}")
|
||||||
|
|
||||||
|
class Result(BaseModel):
|
||||||
|
result : str
|
||||||
|
|
||||||
|
async def single_async_item(
|
||||||
|
api_url,
|
||||||
|
api_key,
|
||||||
|
item,
|
||||||
|
reasoning_effort,
|
||||||
|
temperature,
|
||||||
|
top_p,
|
||||||
|
semaphore_number,
|
||||||
|
model_name,
|
||||||
|
priority,
|
||||||
|
output_schema=None,
|
||||||
|
max_token=4096,
|
||||||
|
print_logs=False,
|
||||||
|
return_reason=False,
|
||||||
|
stop=None,
|
||||||
|
return_used_token=False,
|
||||||
|
timeout=300,
|
||||||
|
):
|
||||||
|
try:
|
||||||
|
async with AsyncOpenAI(
|
||||||
|
base_url=api_url, api_key=api_key
|
||||||
|
) as client:
|
||||||
|
semaphore = asyncio.Semaphore(semaphore_number)
|
||||||
|
async with semaphore:
|
||||||
|
messages = [{"role": "user", "content": item["user_prompt"]}]
|
||||||
|
if item.get("system_prompt"):
|
||||||
|
messages.insert(
|
||||||
|
0, {"role": "system", "content": item["system_prompt"]}
|
||||||
|
)
|
||||||
|
if item.get("assistant_prompt"):
|
||||||
|
messages.append(
|
||||||
|
{"role": "assistant", "content": item["assistant_prompt"]}
|
||||||
|
)
|
||||||
|
|
||||||
|
coro = client.chat.completions.parse(
|
||||||
|
model=model_name,
|
||||||
|
messages=messages,
|
||||||
|
temperature=temperature,
|
||||||
|
top_p=top_p,
|
||||||
|
max_tokens=max_token,
|
||||||
|
stop=stop,
|
||||||
|
response_format=output_schema,
|
||||||
|
reasoning_effort=reasoning_effort,
|
||||||
|
extra_body={"priority": priority},
|
||||||
|
)
|
||||||
|
response = await asyncio.wait_for(coro, timeout=timeout)
|
||||||
|
|
||||||
|
if print_logs:
|
||||||
|
print(f"parse response ---- {response}")
|
||||||
|
|
||||||
|
parsed_obj = response.choices[0].message.parsed
|
||||||
|
# print(f'parsed_obj {parsed_obj}')
|
||||||
|
if parsed_obj is None:
|
||||||
|
return {
|
||||||
|
"error": "Failed to parse response",
|
||||||
|
"raw": str(response),
|
||||||
|
}
|
||||||
|
|
||||||
|
parsed_obj = output_schema.model_validate(parsed_obj)
|
||||||
|
# Validate just in case (optional, چون .parse already does it)
|
||||||
|
if return_reason:
|
||||||
|
reasoning_content = response.choices[
|
||||||
|
0
|
||||||
|
].message.reasoning_content
|
||||||
|
if return_used_token:
|
||||||
|
_total_token = response.usage.total_tokens
|
||||||
|
item["llm_output"] = (
|
||||||
|
parsed_obj.model_dump(),
|
||||||
|
str(reasoning_content),
|
||||||
|
int(_total_token),
|
||||||
|
)
|
||||||
|
return item
|
||||||
|
|
||||||
|
item["llm_output"] = (
|
||||||
|
parsed_obj.model_dump(),
|
||||||
|
str(reasoning_content)
|
||||||
|
)
|
||||||
|
return item
|
||||||
|
|
||||||
|
item["llm_output"] = parsed_obj.model_dump()
|
||||||
|
return item
|
||||||
|
|
||||||
|
except asyncio.TimeoutError:
|
||||||
|
print(f"⏳ Timeout on item {item}")
|
||||||
|
return None
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
print(f"⚠️ Error __process_item {item}: {traceback.print_exc()}")
|
||||||
|
return None
|
||||||
|
|
||||||
async def main():
|
async def main():
|
||||||
with open('./leader_data/khamenei_messages_4.json', 'r', encoding='utf-8') as file:
|
with open('./leader_data/khamenei_messages_4.json', 'r', encoding='utf-8') as file:
|
||||||
data = json.load(file)
|
data = json.load(file)
|
||||||
|
|
@ -221,9 +319,33 @@ async def main():
|
||||||
print(f'all_paragraphs: {all_paragraphs}')
|
print(f'all_paragraphs: {all_paragraphs}')
|
||||||
print('---------------------------------------------')
|
print('---------------------------------------------')
|
||||||
|
|
||||||
|
async def oss_test():
|
||||||
|
item = {}
|
||||||
|
item['assistant_prompt'] = "تو یک دستیار خبره در زمینه تدوین متون علمی هستی"
|
||||||
|
item['system_prompt'] = "پاسخ ها فقط باید علمی باشند و سبک نگارش طنز، سرگرمی، ادبی،احساسی و ... قابل قبول نیست."
|
||||||
|
item['user_prompt'] = "ابعاد مختلف علوم اجتماعی محاسباتی کدام است؟"
|
||||||
|
response = await single_async_item(
|
||||||
|
api_url="http://2.188.15.102:8001/v1/",
|
||||||
|
api_key="EMPTY",
|
||||||
|
item=item,
|
||||||
|
reasoning_effort="medium",
|
||||||
|
temperature=0.1,
|
||||||
|
top_p=1,
|
||||||
|
semaphore_number=1,
|
||||||
|
model_name="gpt-oss-120b",
|
||||||
|
priority=1,
|
||||||
|
output_schema=Result,
|
||||||
|
max_token=None,
|
||||||
|
return_reason=True,
|
||||||
|
return_used_token=True,
|
||||||
|
timeout=300
|
||||||
|
)
|
||||||
|
print(response['llm_output'])
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
asyncio.run(main())
|
# asyncio.run(main())
|
||||||
|
asyncio.run(oss_test())
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user