
# MediSearch API Client

[MediSearch](https://medisearch.io/) API Client provides a Python interface for the MediSearch API. Easily send user messages and receive LLM responses, articles, and handle any errors that might arise.

## Installation

```bash
pip install medisearch_client
```

## Usage

### Initialization

Initialize the client with your API key (email founders@medisearch.io with your use
  case if you do not already have one):

```python
from medisearch_client import MediSearchClient

client = MediSearchClient(api_key="your_api_key")
```

### Sending User Messages and Making Follow-Up Queries

To send a user message, first generate a unique conversation id (`your_conversation_id` below) for your conversation. Then, you may call the client as:

```python
responses = client.send_user_message(
    conversation=["Stomach pain"],
    conversation_id="your_conversation_id",
    should_stream_response=True
)

for response in responses:
    print(response)
```

#### Conversation Structure

The `conversation` parameter is a list of strings where user and chatbot messages alternate. For follow-up queries, append the user's new message to the end of the list.

Example:

```python
conversation=[
    "What is diabetes?",
    "Diabetes is...",
    "How is it treated?"
]
```

Always ensure the last message in the conversation is from the user.

#### Streaming Option

The `should_stream_response` flag controls how the client receives responses:

- `True`: Stream the responses as they arrive. This is useful if you want to process each part of the response separately or if you want to display it to the user piece by piece.
  
- `False`: Wait until all parts of the response are collected and then return them as a list.

### Supported Languages

MediSearch supports:

- English
- French
- Spanish
- German
- Hindi
- Chinese
- Japanese
- Slovak
- Arabic

Set the `language` parameter in `send_user_message` accordingly.

#### Output structure
The output of the call will be a list of MediSearch events. Please see [our docs](https://mpmisko.notion.site/MediSearch-API-documentation-d89474b50e6c4e4a80b2dcd2bdc8ed69) for a detailed description of all the events.

## Error Handling

MediSearch API might occasionally return error events. Here are the common `error_code` values you might encounter:

- `error_auth`: Incorrect API key.


- `error_internal`: An internal bug on MediSearch's end. If persistent, consider contacting MediSearch support (email founders@medisearch.io).
  
- `error_llm`: Issues with the Large Language Model (LLM). If persistent, consider contacting MediSearch support (email founders@medisearch.io).
  
- `error_not_enough_articles`: No relevant articles were found for the query. Might occur for non-medical or nonsensical queries. If you think we should have found relevant articles for a given query, please email founders@medisearch.io.
  
- `error_out_of_tokens`: The conversation's context became too lengthy. Typically occurs if the conversation consists of about 8 back-and-forths. Start a new conversation to resolve.

For a deeper dive into the API's capabilities and events, refer to our [docs](https://mpmisko.notion.site/MediSearch-API-055d82267e06457cbf4e0be0cd628677).
