microsoft / autogen

A programming framework for agentic AI đŸ¤–
https://microsoft.github.io/autogen/
Creative Commons Attribution 4.0 International
30.79k stars 4.49k forks source link

An error occurred: Error code: 500 - {'error': {'message': 'The model produced invalid content. Consider modifying your prompt if you are seeing this error persistently.', 'type': 'model_error', 'param': None, 'code': None}}[Issue]: #3247

Open Adibahaq opened 1 month ago

Adibahaq commented 1 month ago

Describe the issue

Hello, I am trying to use Autogen for this multiagent healthcare system. The code looks like this:

config_list = [ { "model": "gpt-3.5-turbo-16k", "api_key": "key", "api_type": "openai", }

]

llm_config = { "timeout": 600, "seed": 42, "config_list": config_list, "temperature": 0, "max_tokens": 400, }

Define the agents

PrimaryAG = autogen.AssistantAgent( name="Primary_Agent", llm_config=llm_config, system_message="""You are an assistant that processes patient reports. Extract the following general information from the given text:

Also, identify and extract the following sections:

CKDAG = autogen.AssistantAgent( name="CKD_Agent", llm_config=llm_config, system_message="Chronic kidney disease specialist agent", code_execution_config={"work_dir": "web", "use_docker": False} )

HFAG = autogen.AssistantAgent( name="HF_Agent", llm_config=llm_config, system_message="Heart failure specialist agent", code_execution_config={"work_dir": "web", "use_docker": False} )

LABAG = autogen.AssistantAgent( name="LAB_Agent", llm_config=llm_config, system_message=""" You are a medical lab specialist. In a conversation when the Primary agent identifies the Lab Results from a patient report, you analyze the informaion in that section (only that section), and provide a comprehensive and concise analysis of the patient's condition based on the lab results or tests""", code_execution_config={"work_dir": "web", "use_docker": False} )

MEDAG = autogen.AssistantAgent( name="Med_Agent", llm_config=llm_config, system_message=""" You are a drugs and medicine specialist with extensive pharmaceutical knowledge. In a conversation when the Primary agent identifies the Medications section from a patient report, you analyze the information in that section (only that section), and provide a comprehensive and concise analysis. Include the following in your analysis:

Critic = autogen.AssistantAgent( name="Critic_Agent", llm_config=llm_config, system_message="You are a critic, known for your thoroughness and commitment to standards. Your task is to scrutinize answers for correctness and any harmful elements, and provide short feedback", code_execution_config={"work_dir": "web", "use_docker": False} )

Initialize the UserProxyAgent

user_proxy = autogen.UserProxyAgent( name="user_proxy", code_execution_config={"work_dir": "web", "use_docker": False}, llm_config=llm_config, )

Function to reflect on messages

def reflection_message(messages, sender, config): return f"Reflect and provide critique on the following answers:\n\n" + "\n".join( f"Role: {msg['role']}\nContent: {msg['content']}" for msg in messages )

Function to handle user input

def handle_user_input(conversation_history, user_input): if user_input.lower() == "exit": print("Exiting the conversation.") return False

conversation_history.append({"role": "user", "content": user_input})

# Get the response from the Primary Agent
primary_response = user_proxy.initiate_chat(
    recipient=PrimaryAG,
    message=user_input,
    context=conversation_history,
    max_turns=1,
    summary_method="last_msg"
)

# Extract chat history from Primary Agent's response
primary_response_history = primary_response.chat_history

# Print the type of primary_response content
print(f"Type of primary_response_history: {type(primary_response_history)}")
print(f"Primary Response History: {primary_response_history}")

# Extract the content of the last message from the chat history
primary_response_content = primary_response_history[-1]['content'] if primary_response_history else ""

# Determine which agents to invoke based on the primary response content
agents_to_invoke = []
if "Lab Results" in primary_response_content:
    agents_to_invoke.append(LABAG)
if "CKD" in primary_response_content:
    agents_to_invoke.append(CKDAG)
if "Heart Failure" in primary_response_content:
    agents_to_invoke.append(HFAG)
if "Medication" in primary_response_content:
    agents_to_invoke.append(MEDAG)

# Ensure Primary Agent's response is always first
if not agents_to_invoke:
    agents_to_invoke.append(PrimaryAG)

# Register nested chats for Critic agent
group_chat = autogen.GroupChat(
    agents=agents_to_invoke,
    messages=conversation_history,
    allow_repeat_speaker=False,
    max_round=len(agents_to_invoke) + 1,
    send_introductions=True,
)

group_chat_manager = autogen.GroupChatManager(
    groupchat=group_chat,
    llm_config=llm_config,
)

response = user_proxy.initiate_chat(
    recipient=group_chat_manager,
    message=user_input,
    context=conversation_history,
    max_turns=1,
    summary_method="last_msg"
)

conversation_history.append({"role": "agent", "content": response})

# Critic agent feedback
critic_response = user_proxy.initiate_chat(
    recipient=Critic,
    message=reflection_message(conversation_history, None, None),
    context=conversation_history,
    max_turns=1,
    summary_method="last_msg"
)

conversation_history.append({"role": "critic", "content": critic_response})

print(f"Agent Response: {response}")
print(f"Critic Feedback: {critic_response}")

return True

Main conversation loop

def main(): print("Start your conversation. Type 'exit' to end.") conversation_history = [] while True: user_input = input("User: ") if not handle_user_input(conversation_history, user_input): break

if name == "main": main()

Can anyone please tell me why I am getting this error message: An error occurred: Error code: 500 - {'error': {'message': 'The model produced invalid content. Consider modifying your prompt if you are seeing this error persistently.', 'type': 'model_error', 'param': None, 'code': None}}

I get an output first, which looks like this:

Primary_Agent (to user_proxy):

Patient Name: Not mentioned Age: 41 years old Sex: Male Admission Date: November 3, 2203 Discharge Date: November 12, 2203

Sections:


Next speaker: LAB_Agent

LAB_Agent (to chat_manager):

Based on the lab results provided in the patient report, here is a comprehensive analysis of the patient's condition:

  1. Hematology:

    • The patient has anemia with a low hemoglobin level (9.2 g/dl) and hematocrit (28.6%). This could be due to chronic disease or blood loss from the gastrointestinal bleeding.
    • The white blood cell count (6.2) and platelet count (295) are within normal range.
  2. Chemistry:

    • The patient has slightly elevated lactate levels (3.2 mg/dl), which could indicate tissue hypoxia or impaired oxygen utilization.
    • The glucose level is elevated (126 mg/dl), indicating possible diabetes or stress-induced hyperglycemia.
    • The kidney function tests show elevated creatinine (1.8 mg/dl) and decreased estimated glomerular filtration rate (eGFR), suggesting impaired kidney function.
    • The electrolyte levels (sodium, potassium, chloride, total CO2) are within normal range.
  3. Cardiac Markers:

    • The troponin level is very low (<0.01), indicating no evidence of acute myocardial infarction.
    • CK (CPK) level is normal (29 U/L), suggesting no significant muscle damage.
  4. Coagulation:

    • The prothrombin time (PT) is slightly prolonged (15.1 seconds), indicating a mild impairment in the clotting process.
    • The partial thromboplastin time (PTT) is within normal range (38.2 seconds).
    • The international normalized ratio (INR) is slightly elevated (1.4), indicating a mild impairment in the clotting process.
  5. Other findings:

    • The pericardial fluid analysis shows elevated total protein (6.1 g/dl) and lactate dehydrogenase (LDH) levels (650 U/L), indicating inflammation or infection in the pericardium.
    • The white blood cell count in the pericardial fluid (2,122) is elevated, suggesting an inflammatory process.
    • The echocardiogram findings indicate a large pericardial effusion with signs of tamponade physiology, as well as tricuspid regurgitation and mild primary pulmonary hypertension.

Based on these results, the patient's primary diagnosis is cardiac tamponade, which is a medical emergency requiring immediate intervention. The pericardial effusion is likely the cause of the patient's symptoms of shortness of breath and chest pain. The underlying cause of the effusion needs further investigation, as no specific cause was identified in the report.

Other notable findings include anemia, possible gastrointestinal bleeding, impaired kidney function, and elevated glucose levels. These findings may require further evaluation and management.

The patient's medical history of HIV/AIDS, previous endocarditis, and tricuspid regurgitation are important factors to consider in the overall management and treatment plan.

Further monitoring of the patient's cardiac function, including regular echocardiograms, is recommended to assess for fluid reaccumulation and to evaluate the severity of tricuspid regurgitation.

Overall, the patient's condition is complex and requires a multidisciplinary approach involving cardiology, infectious disease, gastroenterology, and orthopedics to address the various medical issues identified in the report.


Next speaker: Med_Agent


and then I get the error message.

Steps to reproduce

No response

Screenshots and logs

No response

Additional Information

No response

davorrunje commented 2 weeks ago

@Adibahaq This issue is most likely the following error introduced by more strict JSON checking:

https://community.openai.com/t/error-the-model-produced-invalid-content/747511

I will create a pull request shortly and I would appreciate if you could test your example against it.

davorrunje commented 2 weeks ago

@Adibahaq please check if your code works with the fix in PR #3429

LittleLittleCloud commented 2 weeks ago

Also see this issue from .net end. Will push a fix in AutoGen.Net as well