|
import erniebot |
|
|
|
aclient = None |
|
|
|
client = None |
|
|
|
import os |
|
|
|
def normalize2uaua_ernie( message, if_replace_system = False ): |
|
new_message = [] |
|
last_role = "" |
|
|
|
for msg in message: |
|
role = msg["role"] |
|
if if_replace_system and role == "system": |
|
role = "user" |
|
msg["role"] = role |
|
|
|
if last_role == role: |
|
new_message[-1]["content"] = new_message[-1]["content"] + "\n" + msg["content"] |
|
else: |
|
last_role = role |
|
new_message.append( msg ) |
|
|
|
return new_message |
|
|
|
def init_client(): |
|
|
|
|
|
global client |
|
|
|
|
|
api_key = os.getenv("ERNIE_ACCESS_TOKEN") |
|
if api_key is None: |
|
raise ValueError("环境变量'ERNIE_ACCESS_TOKEN'未设置,请确保已经定义了API密钥") |
|
erniebot.api_type = "aistudio" |
|
erniebot.access_token = api_key |
|
client = erniebot |
|
|
|
def get_response( message, model_name = "ernie-4.0" ): |
|
if client is None: |
|
init_client() |
|
|
|
message_ua = normalize2uaua_ernie(message, if_replace_system = True) |
|
|
|
response = client.ChatCompletion.create(\ |
|
model=model_name,\ |
|
messages = message_ua, \ |
|
temperature = 0.1 ) |
|
return response.get_result() |
|
|
|
import json |
|
import asyncio |
|
from erniebot_agent.chat_models import ERNIEBot |
|
from erniebot_agent.memory import HumanMessage, AIMessage, SystemMessage, FunctionMessage |
|
|
|
def init_aclient(model="ernie-4.0"): |
|
|
|
|
|
global aclient |
|
|
|
api_key = os.getenv("ERNIE_ACCESS_TOKEN") |
|
if api_key is None: |
|
raise ValueError("环境变量'ERNIE_ACCESS_TOKEN'未设置。请确保已经定义了API密钥。") |
|
os.environ["EB_AGENT_ACCESS_TOKEN"] = api_key |
|
aclient = ERNIEBot(model=model) |
|
|
|
|
|
|
|
async def async_get_response( message, model="ernie-4.0" ): |
|
if aclient is None: |
|
init_aclient(model=model) |
|
|
|
messages = [] |
|
system_message = None |
|
message_ua = normalize2uaua_ernie(message, if_replace_system = False) |
|
print(message_ua) |
|
for item in message_ua: |
|
if item["role"] == "user": |
|
messages.append(HumanMessage(item["content"])) |
|
elif item["role"] == "system": |
|
system_message = SystemMessage(item["content"]) |
|
else: |
|
messages.append(AIMessage(item["content"])) |
|
if system_message: |
|
ai_message = await aclient.chat(messages=messages, temperature = 0.1) |
|
else: |
|
ai_message = await aclient.chat(messages=messages, system=system_message.content, temperature = 0.1) |
|
|
|
return ai_message.content |