“openai.BadRequestError: Invalid role” error while using Ollama + LiteLLM + Autogen

简介

我正在运行一个带有LiteLLM代理的ollama服务器。我的目标是使用Autogen设计一个群聊。指定的代理包括:

  • User_Proxy:提问
  • SQL_Generator:生成SQL
  • SQL_Runner:运行SQL(工具调用)
  • Result_Validator:验证结果

问题

  1. 群聊在SQL_Runner从数据库返回数据之前运行正常。但当预期下一个发言者是Result_Validator时,SQL_Generator被指定为下一个发言者,尽管我在系统消息中明确指出SQL_Runner应该将结果传递给Result_Validator

  2. 由于某些原因,SQL_Generator代理的角色在SQL_Runner活动后仍然保持为tool,而不是[system, user, assistant]中的一个,这导致了以下错误:

***** Response from calling tool (call_6493f510-d318-4e85-b552-8963bece5fcb) *****[    // data from DB]**********************************************************************************Next speaker: SQL_Generator// error traceopenai.BadRequestError: Error code: 400 - {'error': {'message': 'invalid role: tool, role must be one of [system, user, assistant]', 'type': 'api_error', 'param': None, 'code': None}}

代码

LLM配置
llama3_config_list = [    {        "model": "llama3:latest",        "base_url": f"{OLLAMA_BASE_URL}:{OLLAMA_PORT}/v1",        "api_key": "ollama",    }]litellm_config_list = [    {        "model": "NotRequired",        "api_key": "NotRequired",        "base_url": f"{LITELLM_BASE_URL}:{LITELLM_PORT}",        "price": [0, 0],    }]local_llm_config = {"config_list": litellm_config_list, "cache_seed": None}
群聊
user_proxy = autogen.UserProxyAgent(    name=Role.USER_PROXY,    system_message=Prompt.USER_PROXY,    human_input_mode="NEVER",    is_termination_msg=is_termination_msg,    code_execution_config={"use_docker": False, "work_dir": "test"},)sql_generator = autogen.AssistantAgent(    name=Role.SQL_GENERATOR,    system_message=Prompt.SQL_GENERATOR,    llm_config=get_custom_llm_config(config_list=llama3_config_list),)sql_runner = autogen.AssistantAgent(    name=Role.SQL_RUNNER,    system_message=Prompt.SQL_RUNNER,    llm_config=local_llm_config,)@user_proxy.register_for_execution()@sql_runner.register_for_llm(description="SQL query runner")def run_sql(sql: Annotated[str, "SQL query string to run"]) -> str:    with PostgresManager() as db:        db.get_rds_connection()        return db.run_sql(sql)result_validator = autogen.AssistantAgent(    name=Role.RESULT_VALIDATOR,    system_message=Prompt.RESULT_VALIDATOR,    llm_config=get_custom_llm_config(config_list=llama3_config_list))groupchat = autogen.GroupChat(    agents=[        user_proxy,        sql_generator,        sql_runner,        result_validator,    ],    messages=[],)manager = autogen.GroupChatManager(groupchat=groupchat)user_proxy.initiate_chat(manager, message=question)

对于问题1,我已经尝试将autogen.GroupChatspeaker_selection_method设置为"round_robin",但结果没有任何变化。

对于问题2,我猜测如果能以某种方式防止代理角色被缓存,可能会解决这个问题,但我实在找不到具体的方法。


回答:

我遇到这个错误是因为我的ollama版本不支持工具调用。

在升级ollama服务器版本(至0.4.2)后,现在角色”tool”可用。

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注