Consult with AI Neurologist with Lyzr Automata, Streamlit and OpenAI
Tags: openai, healthcare, lyzr, ai
Published: 2024-04-30 11:23:27

In the ever-evolving landscape of healthcare technology, the integration of artificial intelligence (AI) has revolutionized various medical domains. One such application is the development of consultation tools that aid healthcare professionals in providing efficient and accurate assessments. In this blog post, we'll explore how to build a Neurology Consultant app using Streamlit and OpenAI, enabling streamlined interactions and insights.

{% embed https://discord.com/invite/KnuSUbhbkh %}

**Setting Up the Environment**
**Imports:**

```
pip install lyzr_automata streamlit
import streamlit as st
from lyzr_automata.ai_models.openai import OpenAIModel
from lyzr_automata import Agent,Task
from lyzr_automata.pipelines.linear_sync_pipeline import LinearSyncPipeline
from PIL import Image
from dotenv import load_dotenv
import os
from prompt import example
load_dotenv()
api = os.getenv("OPENAI_API_KEY")
```
Imports necessary libraries like streamlit, dotenv, PIL, and custom libraries for Lyzr Automata.
Loads environment variables using load_dotenv (likely containing the OpenAI API key).
load_dotenv() is called to load environment variables from a .env file (likely containing your OpenAI API key).
api = os.getenv("OPENAI_API_KEY") retrieves the API key from the environment variable.

**OpenAI Model Setup:**

```
open_ai_text_completion_model = OpenAIModel(
    api_key=api,
    parameters={
        "model": "gpt-4-turbo-preview",
        "temperature": 0.2,
        "max_tokens": 1500,
    },
)
```
The OpenAIModel class from lyzr_automata is used to create an instance of the OpenAI text completion model (open_ai_text_completion_model).
It sets the API key, model name ("gpt-4-turbo-preview"), temperature (0.2), and maximum output length (1500 tokens).

**User Input:**

```
query=st.text_area("Enter your conversation: ")
```
query: Creates a text area for the user to input their conversation prompt.

**Neurology Consultation Function:**

```
def neurology_consult(query):

    neurology_agent = Agent(
            role="Neurology expert",
            prompt_persona=f"You are an Expert neurologist.your task is to create medical note from {query}."
        )

    neurology_task = Task(
        name="Neurology Consult",
        model=open_ai_text_completion_model,
        agent=neurology_agent,
        instructions=prompt,
    )

    output = LinearSyncPipeline(
        name="Neurology Consult Pipline",
        completion_message="pipeline completed",
        tasks=[
              neurology_task
        ],
    ).run()

    answer = output[0]['task_output']

    return answer
```
def neurology_consult(query):: Defines a function neurology_consult that takes query as input.
**Agent Definition:**
neurology_agent = Agent(...): Creates an Agent object with the following properties:
role: Sets the agent's role to "Neurology expert".
prompt_persona: Defines the agent's persona using a prompt string, indicating its task is to create a medical note based on the query.
**Task Definition:**
neurology_task = Task(...): Creates a Task object with the following properties:
name: Sets the task name to "Neurology Consult".
model: Sets the model to use (the open_ai_text_completion_model created earlier)
**Conversation Pipeline:**
LinearSyncPipeline(...): Creates a LinearSyncPipeline object, likely from lyzr_automata, which manages the conversation flow between the user and the neurology agent.
name: Sets the pipeline name to "Neurology Consult Pipline".
completion_message: Sets the message displayed when the pipeline finishes ("pipeline completed").
tasks: Defines a list of tasks within the pipeline, in this case, it only has one task:
neurology_task: The previously defined task for the neurology consultation.
.run(): Executes the LinearSyncPipeline, running the neurology consultation task.

**Output:**

```
if st.button("Consult"):
    solution = neurology_consult(query)
    st.markdown(solution)
```
if st.button("Consult"):: Creates a button labeled "Consult". When the button is clicked, the following code executes.
solution = neurology_consult(query): Calls the neurology_consult function with the user's input (query) and stores the generated medical note in the solution variable.
st.markdown(solution): Displays the generated medical note as formatted markdown text in the Streamlit app.

try it now: [https://lyzr-neurology-consultant.streamlit.app/](https://lyzr-neurology-consultant.streamlit.app/)
For more information explore the website:[ Lyzr](https://www.lyzr.ai/)