test / llm /yi_moe_api.py
01du
Add application file
cdbb2b2
import requests
import json
import logger
import time
def yi_moe(api_key,
model="moyi-chat-v03-sglang",
user_content="如何下载github上的项目?",
system_content=None):
url ="http://10.2.5.29:30869/v1/chat/completions"
headers = {
"Authorization": "Basic ZjIyMDIwYjRkMTIyM2UyNGI4NjVlMWIxZWI0YzAzZTM6NWJjS01PbENMTDRTV1MxaERkSHlTRzViSTJCd3psR1A=" + api_key,
"Content-Type": "application/json"
}
data = {
"model": model,
"messages": [
{
"role": "system",
"content": system_content
},
{
"role": "user",
"content": user_content
}
],
"temperature": 0.7,
"stream": False,
"max_tokens": 4096
}
max_retries = 5
retry_count = 0
while retry_count < max_retries:
try:
response = requests.post(url, json=data, headers=headers, timeout=60)
if response.status_code == 200:
response = response.json()
text = response["choices"][0]["message"]["content"]
return text
else:
logger.warning(f"{response.status_code}, {response.text}")
retry_count += 1
time.sleep(2)
logger.info(f"Retrying... attempt {retry_count}")
except Exception as e:
logger.error(e)
retry_count += 1
time.sleep(2)
logger.info(f"Retrying... attempt {retry_count}")
return "error"