From dd9d67e7b32d15875bd4b1a8e50d2b637db9f537 Mon Sep 17 00:00:00 2001 From: ascender1729 <pavankumar.dubasi2019@gmail.com> Date: Fri, 16 May 2025 21:55:55 +0530 Subject: [PATCH 1/5] fix: Update 4o_mini_demo.py to use GPT-4o-mini model - Replace Ollama local models with OpenAI GPT-4o-mini - Resolve context spam and extraneous output issues - Improve output formatting for medical coding diagnosis - Closes #771 --- examples/4o_mini_demo.py | 249 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 249 insertions(+) create mode 100644 examples/4o_mini_demo.py diff --git a/examples/4o_mini_demo.py b/examples/4o_mini_demo.py new file mode 100644 index 00000000..04f54140 --- /dev/null +++ b/examples/4o_mini_demo.py @@ -0,0 +1,249 @@ +""" +- For each diagnosis, pull lab results, +- egfr +- for each diagnosis, pull lab ranges, +- pull ranges for diagnosis + +- if the diagnosis is x, then the lab ranges should be a to b +- train the agents, increase the load of input +- medical history sent to the agent +- setup rag for the agents +- run the first agent -> kidney disease -> don't know the stage -> stage 2 -> lab results -> indicative of stage 3 -> the case got elavated -> +- how to manage diseases and by looking at correlating lab, docs, diagnoses +- put docs in rag -> +- monitoring, evaluation, and treatment +- can we confirm for every diagnosis -> monitoring, evaluation, and treatment, specialized for these things +- find diagnosis -> or have diagnosis, -> for each diagnosis are there evidence of those 3 things +- swarm of those 4 agents, -> +- fda api for healthcare for commerically available papers +- + +""" + +from datetime import datetime + +from swarms import Agent, AgentRearrange, create_file_in_folder + + +chief_medical_officer = Agent( + agent_name="Chief Medical Officer", + system_prompt="""You are the Chief Medical Officer coordinating a team of medical specialists for viral disease diagnosis. + Your responsibilities include: + - Gathering initial patient symptoms and medical history + - Coordinating with specialists to form differential diagnoses + - Synthesizing different specialist opinions into a cohesive diagnosis + - Ensuring all relevant symptoms and test results are considered + - Making final diagnostic recommendations + - Suggesting treatment plans based on team input + - Identifying when additional specialists need to be consulted + - For each diferrential diagnosis provide minimum lab ranges to meet that diagnosis or be indicative of that diagnosis minimum and maximum + + Format all responses with clear sections for: + - Initial Assessment (include preliminary ICD-10 codes for symptoms) + - Differential Diagnoses (with corresponding ICD-10 codes) + - Specialist Consultations Needed + - Recommended Next Steps + + + """, + model_name="gpt-4o-mini", + max_loops=1, +) + +virologist = Agent( + agent_name="Virologist", + system_prompt="""You are a specialist in viral diseases. For each case, provide: + + Clinical Analysis: + - Detailed viral symptom analysis + - Disease progression timeline + - Risk factors and complications + + Coding Requirements: + - List relevant ICD-10 codes for: + * Confirmed viral conditions + * Suspected viral conditions + * Associated symptoms + * Complications + - Include both: + * Primary diagnostic codes + * Secondary condition codes + + Document all findings using proper medical coding standards and include rationale for code selection.""", + model_name="gpt-4o-mini", + max_loops=1, +) + +internist = Agent( + agent_name="Internist", + system_prompt="""You are an Internal Medicine specialist responsible for comprehensive evaluation. + + For each case, provide: + + Clinical Assessment: + - System-by-system review + - Vital signs analysis + - Comorbidity evaluation + + Medical Coding: + - ICD-10 codes for: + * Primary conditions + * Secondary diagnoses + * Complications + * Chronic conditions + * Signs and symptoms + - Include hierarchical condition category (HCC) codes where applicable + + Document supporting evidence for each code selected.""", + model_name="gpt-4o-mini", + max_loops=1, +) + +medical_coder = Agent( + agent_name="Medical Coder", + system_prompt="""You are a certified medical coder responsible for: + + Primary Tasks: + 1. Reviewing all clinical documentation + 2. Assigning accurate ICD-10 codes + 3. Ensuring coding compliance + 4. Documenting code justification + + Coding Process: + - Review all specialist inputs + - Identify primary and secondary diagnoses + - Assign appropriate ICD-10 codes + - Document supporting evidence + - Note any coding queries + + Output Format: + 1. Primary Diagnosis Codes + - ICD-10 code + - Description + - Supporting documentation + 2. Secondary Diagnosis Codes + - Listed in order of clinical significance + 3. Symptom Codes + 4. Complication Codes + 5. Coding Notes""", + model_name="gpt-4o-mini", + max_loops=1, +) + +synthesizer = Agent( + agent_name="Diagnostic Synthesizer", + system_prompt="""You are responsible for creating the final diagnostic and coding assessment. + + Synthesis Requirements: + 1. Integrate all specialist findings + 2. Reconcile any conflicting diagnoses + 3. Verify coding accuracy and completeness + + Final Report Sections: + 1. Clinical Summary + - Primary diagnosis with ICD-10 + - Secondary diagnoses with ICD-10 + - Supporting evidence + 2. Coding Summary + - Complete code list with descriptions + - Code hierarchy and relationships + - Supporting documentation + 3. Recommendations + - Additional testing needed + - Follow-up care + - Documentation improvements needed + + Include confidence levels and evidence quality for all diagnoses and codes.""", + model_name="gpt-4o-mini", + max_loops=1, +) + +# Create agent list +agents = [ + chief_medical_officer, + virologist, + internist, + medical_coder, + synthesizer, +] + +# Define diagnostic flow +flow = f"""{chief_medical_officer.agent_name} -> {virologist.agent_name} -> {internist.agent_name} -> {medical_coder.agent_name} -> {synthesizer.agent_name}""" + +# Create the swarm system +diagnosis_system = AgentRearrange( + name="Medical-coding-diagnosis-swarm", + description="Comprehensive medical diagnosis and coding system", + agents=agents, + flow=flow, + max_loops=1, + output_type="all", +) + + +def generate_coding_report(diagnosis_output: str) -> str: + """ + Generate a structured medical coding report from the diagnosis output. + """ + timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S") + + report = f"""# Medical Diagnosis and Coding Report + Generated: {timestamp} + + ## Clinical Summary + {diagnosis_output} + + ## Coding Summary + ### Primary Diagnosis Codes + [Extracted from synthesis] + + ### Secondary Diagnosis Codes + [Extracted from synthesis] + + ### Symptom Codes + [Extracted from synthesis] + + ### Procedure Codes (if applicable) + [Extracted from synthesis] + + ## Documentation and Compliance Notes + - Code justification + - Supporting documentation references + - Any coding queries or clarifications needed + + ## Recommendations + - Additional documentation needed + - Suggested follow-up + - Coding optimization opportunities + """ + return report + + +if __name__ == "__main__": + # Example patient case + patient_case = """ + Patient: 45-year-old White Male + + Lab Results: + - egfr + - 59 ml / min / 1.73 + - non african-american + + """ + + # Add timestamp to the patient case + case_info = f"Timestamp: {datetime.now()}\nPatient Information: {patient_case}" + + # Run the diagnostic process + diagnosis = diagnosis_system.run(case_info) + + # Generate coding report + coding_report = generate_coding_report(diagnosis) + + # Create reports + create_file_in_folder( + "reports", "medical_diagnosis_report.md", diagnosis + ) + create_file_in_folder( + "reports", "medical_coding_report.md", coding_report + ) From 6698bd3c8b6ff01b11eadd774950c9bebc63fecb Mon Sep 17 00:00:00 2001 From: ascender1729 <pavankumar.dubasi2019@gmail.com> Date: Sat, 17 May 2025 01:42:31 +0530 Subject: [PATCH 2/5] fix: Update ollama_demo.py to use LiteLLM format - Update model configuration to use LiteLLM format (ollama/llama3.2) - Standardize model handling with 4o_mini_demo.py - Improve output formatting and consistency - Fix context spam issues --- examples/ollama_demo.py | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/examples/ollama_demo.py b/examples/ollama_demo.py index 4d1d41ef..bf369a56 100644 --- a/examples/ollama_demo.py +++ b/examples/ollama_demo.py @@ -24,9 +24,6 @@ from datetime import datetime from swarms import Agent, AgentRearrange, create_file_in_folder -from swarm_models import OllamaModel - -model = OllamaModel(model_name="llama3.2") chief_medical_officer = Agent( agent_name="Chief Medical Officer", @@ -49,7 +46,7 @@ chief_medical_officer = Agent( """, - llm=model, + model_name="ollama/llama3.2", max_loops=1, ) @@ -73,7 +70,7 @@ virologist = Agent( * Secondary condition codes Document all findings using proper medical coding standards and include rationale for code selection.""", - llm=model, + model_name="ollama/llama3.2", max_loops=1, ) @@ -98,7 +95,7 @@ internist = Agent( - Include hierarchical condition category (HCC) codes where applicable Document supporting evidence for each code selected.""", - llm=model, + model_name="ollama/llama3.2", max_loops=1, ) @@ -129,7 +126,7 @@ medical_coder = Agent( 3. Symptom Codes 4. Complication Codes 5. Coding Notes""", - llm=model, + model_name="ollama/llama3.2", max_loops=1, ) @@ -157,7 +154,7 @@ synthesizer = Agent( - Documentation improvements needed Include confidence levels and evidence quality for all diagnoses and codes.""", - llm=model, + model_name="ollama/llama3.2", max_loops=1, ) From 5a9b56223edb60a4e5a5b4cb18136de362f8497c Mon Sep 17 00:00:00 2001 From: Kye Gomez <kye@swarms.world> Date: Fri, 16 May 2025 22:44:41 -0700 Subject: [PATCH 3/5] cerebas example md --- cerebas_example.py | 13 ++++ docs/mkdocs.yml | 1 + docs/swarms/models/cerebas_example.md | 86 +++++++++++++++++++++++++++ 3 files changed, 100 insertions(+) create mode 100644 cerebas_example.py create mode 100644 docs/swarms/models/cerebas_example.md diff --git a/cerebas_example.py b/cerebas_example.py new file mode 100644 index 00000000..c42a75ab --- /dev/null +++ b/cerebas_example.py @@ -0,0 +1,13 @@ +from swarms.structs.agent import Agent + +agent = Agent( + agent_name="Financial-Analysis-Agent", + agent_description="Personal finance advisor agent", + max_loops=4, + model_name="cerebras/llama3-70b-instruct", + dynamic_temperature_enabled=True, + interactive=False, + output_type="all", +) + +agent.run("Conduct an analysis of the best real undervalued ETFs") diff --git a/docs/mkdocs.yml b/docs/mkdocs.yml index 5c379fc2..062c93c1 100644 --- a/docs/mkdocs.yml +++ b/docs/mkdocs.yml @@ -347,6 +347,7 @@ nav: - OpenAIChat: "swarms/models/openai.md" - OpenAIFunctionCaller: "swarms/models/openai_function_caller.md" - Groq: "swarms/models/groq.md" + - Cerebras: "swarms/models/cerebas_example.md" - MultiModal Models: - BaseMultiModalModel: "swarms/models/base_multimodal_model.md" - Multi Modal Models Available: "swarms/models/multimodal_models.md" diff --git a/docs/swarms/models/cerebas_example.md b/docs/swarms/models/cerebas_example.md new file mode 100644 index 00000000..44134e84 --- /dev/null +++ b/docs/swarms/models/cerebas_example.md @@ -0,0 +1,86 @@ +# Using Cerebras LLaMA with Swarms + +This guide demonstrates how to create and use an AI agent powered by the Cerebras LLaMA 3 70B model using the Swarms framework. + +## Prerequisites + +- Python 3.7+ +- Swarms library installed (`pip install swarms`) + +## Step-by-Step Guide + +### 1. Import Required Module + +```python +from swarms.structs.agent import Agent +``` + +This imports the `Agent` class from Swarms, which is the core component for creating AI agents. + +### 2. Create an Agent Instance + +```python +agent = Agent( + agent_name="Financial-Analysis-Agent", + agent_description="Personal finance advisor agent", + max_loops=4, + model_name="cerebras/llama3-70b-instruct", + dynamic_temperature_enabled=True, + interactive=False, + output_type="all", +) +``` + +Let's break down each parameter: + +- `agent_name`: A descriptive name for your agent (here, "Financial-Analysis-Agent") + +- `agent_description`: A brief description of the agent's purpose + +- `max_loops`: Maximum number of interaction loops the agent can perform (set to 4) + +- `model_name`: Specifies the Cerebras LLaMA 3 70B model to use + +- `dynamic_temperature_enabled`: Enables dynamic adjustment of temperature for varied responses + +- `interactive`: When False, runs without requiring user interaction + +- `output_type`: Set to "all" to return complete response information + +### 3. Run the Agent + +```python +agent.run("Conduct an analysis of the best real undervalued ETFs") +``` + +This command: + +1. Activates the agent + +2. Processes the given prompt about ETF analysis + +3. Returns the analysis based on the model's knowledge + +## Notes + +- The Cerebras LLaMA 3 70B model is a powerful language model suitable for complex analysis tasks + +- The agent can be customized further with additional parameters + +- The `max_loops=4` setting prevents infinite loops while allowing sufficient processing depth + +- Setting `interactive=False` makes the agent run autonomously without user intervention + +## Example Output + +The agent will provide a detailed analysis of undervalued ETFs, including: + +- Market analysis + +- Performance metrics + +- Risk assessment + +- Investment recommendations + +Note: Actual output will vary based on current market conditions and the model's training data. \ No newline at end of file From f21272930f9afeaf81bc88fafbdb8e9da7e5d9f1 Mon Sep 17 00:00:00 2001 From: Kye Gomez <kye@swarms.world> Date: Fri, 16 May 2025 22:55:04 -0700 Subject: [PATCH 4/5] cerebras --- docs/mkdocs.yml | 2 +- docs/swarms/models/{cerebas_example.md => cerebras.md} | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) rename docs/swarms/models/{cerebas_example.md => cerebras.md} (96%) diff --git a/docs/mkdocs.yml b/docs/mkdocs.yml index 062c93c1..faf1f661 100644 --- a/docs/mkdocs.yml +++ b/docs/mkdocs.yml @@ -347,7 +347,7 @@ nav: - OpenAIChat: "swarms/models/openai.md" - OpenAIFunctionCaller: "swarms/models/openai_function_caller.md" - Groq: "swarms/models/groq.md" - - Cerebras: "swarms/models/cerebas_example.md" + - Cerebras: "swarms/models/cerebras.md" - MultiModal Models: - BaseMultiModalModel: "swarms/models/base_multimodal_model.md" - Multi Modal Models Available: "swarms/models/multimodal_models.md" diff --git a/docs/swarms/models/cerebas_example.md b/docs/swarms/models/cerebras.md similarity index 96% rename from docs/swarms/models/cerebas_example.md rename to docs/swarms/models/cerebras.md index 44134e84..8c544cb2 100644 --- a/docs/swarms/models/cerebas_example.md +++ b/docs/swarms/models/cerebras.md @@ -5,8 +5,11 @@ This guide demonstrates how to create and use an AI agent powered by the Cerebra ## Prerequisites - Python 3.7+ + - Swarms library installed (`pip install swarms`) +- Set your ENV key `CEREBRAS_API_KEY` + ## Step-by-Step Guide ### 1. Import Required Module From 103952a5c768b29e9d06405e4b364b92593d54a1 Mon Sep 17 00:00:00 2001 From: Kye Gomez <kye@swarms.world> Date: Fri, 16 May 2025 23:06:00 -0700 Subject: [PATCH 5/5] [fix][remove agent.agent_id and just agent.id --- examples/4o_mini_demo.py | 10 +++++----- swarms/structs/agent.py | 4 +--- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/examples/4o_mini_demo.py b/examples/4o_mini_demo.py index 04f54140..90b40d0a 100644 --- a/examples/4o_mini_demo.py +++ b/examples/4o_mini_demo.py @@ -46,7 +46,7 @@ chief_medical_officer = Agent( """, - model_name="gpt-4o-mini", + model_name="gpt-4o-mini", max_loops=1, ) @@ -70,7 +70,7 @@ virologist = Agent( * Secondary condition codes Document all findings using proper medical coding standards and include rationale for code selection.""", - model_name="gpt-4o-mini", + model_name="gpt-4o-mini", max_loops=1, ) @@ -95,7 +95,7 @@ internist = Agent( - Include hierarchical condition category (HCC) codes where applicable Document supporting evidence for each code selected.""", - model_name="gpt-4o-mini", + model_name="gpt-4o-mini", max_loops=1, ) @@ -126,7 +126,7 @@ medical_coder = Agent( 3. Symptom Codes 4. Complication Codes 5. Coding Notes""", - model_name="gpt-4o-mini", + model_name="gpt-4o-mini", max_loops=1, ) @@ -154,7 +154,7 @@ synthesizer = Agent( - Documentation improvements needed Include confidence levels and evidence quality for all diagnoses and codes.""", - model_name="gpt-4o-mini", + model_name="gpt-4o-mini", max_loops=1, ) diff --git a/swarms/structs/agent.py b/swarms/structs/agent.py index 2e795b7e..a38691b2 100644 --- a/swarms/structs/agent.py +++ b/swarms/structs/agent.py @@ -279,7 +279,6 @@ class Agent: def __init__( self, - agent_id: Optional[str] = agent_id(), id: Optional[str] = agent_id(), llm: Optional[Any] = None, template: Optional[str] = None, @@ -403,7 +402,6 @@ class Agent: **kwargs, ): # super().__init__(*args, **kwargs) - self.agent_id = agent_id self.id = id self.llm = llm self.template = template @@ -2270,7 +2268,7 @@ class Agent: time=time.time(), tokens=total_tokens, response=AgentChatCompletionResponse( - id=self.agent_id, + id=self.id, agent_name=self.agent_name, object="chat.completion", choices=ChatCompletionResponseChoice(