File size: 3,869 Bytes
82bf89e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# Bio RAG Server

A FastAPI-based Biomedical RAG service that supports PubMed retrieval, web search, and vector DB queries, providing intelligent Q&A and document retrieval with streaming responses.

## πŸš€ Features

- **Multi-source retrieval**: PubMed, Web search, personal vector DBs
- **Intelligent Q&A**: RAG-based answers with streaming SSE responses
- **Query rewrite**: Smart multi-query and rewrite to improve recall and precision
- **Primary/backup LLM**: Automatic failover between main and backup providers
- **Internationalization**: Chinese/English responses (87 i18n messages, 8 categories)
- **Logging & tracing**: Full request tracing with correlation IDs
- **CORS**: Easy frontend integration

## πŸ—οΈ Project Structure (partial)

```
bio_rag_server/
β”œβ”€β”€ bio_agent/
β”œβ”€β”€ bio_requests/
β”œβ”€β”€ config/
β”œβ”€β”€ dto/
β”œβ”€β”€ routers/
β”œβ”€β”€ search_service/
β”œβ”€β”€ service/
β”œβ”€β”€ utils/
└── test/
```

## πŸ“‹ Requirements

- Python 3.11+
- LLM providers (OpenAI-compatible or others per your config)

## πŸ› οΈ Setup

### 1) Install dependencies

```bash
pip install -r requirements.txt
```

### 2) Configure environment

Create a `.env` file (see `env_example.txt` for keys):

- `QA_LLM_MAIN_API_KEY`, `QA_LLM_MAIN_BASE_URL`
- `QA_LLM_BACKUP_API_KEY`, `QA_LLM_BACKUP_BASE_URL`
- `REWRITE_LLM_MAIN_API_KEY`, `REWRITE_LLM_MAIN_BASE_URL`
- `REWRITE_LLM_BACKUP_API_KEY`, `REWRITE_LLM_BACKUP_BASE_URL`
- `SERPER_API_KEY` (web search)
- `ENVIRONMENT` (e.g., dev)

### 3) Run the service

```bash
python main.py
```

Service runs at `http://localhost:9487`.

### Run with Docker

```bash
docker build -t bio-rag-server .
docker run --rm -p 9487:9487 --env-file .env bio-rag-server
```

Note: The Dockerfile pre-installs `crawl4ai` and runs basic setup checks during build.

## πŸ“š API

### 1) Document Retrieval

Endpoint: `POST /retrieve`

Request body:
```json
{
  "query": "cancer treatment",
  "top_k": 5,
  "search_type": "keyword",
  "is_rewrite": true,
  "data_source": ["pubmed"],
  "user_id": "user123",
  "pubmed_topk": 30
}
```

Response (example):
```json
[
  {
    "title": "Cancer Treatment Advances",
    "abstract": "Recent advances in cancer treatment...",
    "url": "https://pubmed.ncbi.nlm.nih.gov/...",
    "score": 0.95
  }
]
```

### 2) Streaming Chat (RAG)

Endpoint: `POST /stream-chat`

Request body:
```json
{
  "query": "What are the latest treatments for breast cancer?",
  "is_web": true,
  "is_pubmed": true,
  "language": "en"
}
```

Response: Server-Sent Events (SSE) streaming

### 3) Internationalization

All APIs support i18n via the `language` field:

- `zh` (default)
- `en`

Success response shape:
```json
{
  "success": true,
  "data": [...],
  "message": "Search successful",
  "language": "en"
}
```

Error response shape:
```json
{
  "success": false,
  "error": {
    "code": 500,
    "message": "Search failed",
    "language": "en",
    "details": "..."
  }
}
```

## πŸ“Š Monitoring & Logs

- Log files: `logs/bio_rag_YYYY-MM-DD.log`
- Correlation ID tracing per request
- Processing time recorded via middleware

## πŸ”’ Security

- API key and endpoint configuration via environment variables
- Request logging
- CORS enabled
- Error handling with safe messages

## 🀝 Contributing

1. Fork
2. Create a feature branch (`git checkout -b feature/AmazingFeature`)
3. Commit (`git commit -m 'Add some AmazingFeature'`)
4. Push (`git push origin feature/AmazingFeature`)
5. Open a Pull Request

## πŸ“„ License

MIT (see `LICENSE`).

## πŸ†˜ Support

1. Check Issues
2. Open a new Issue
3. Contact maintainers

## πŸ—ΊοΈ Roadmap

- [ ] More data sources
- [ ] Auth & permissions
- [ ] Vector search optimization
- [ ] More LLM providers
- [ ] Result caching
- [ ] API rate limiting

---

Note: Ensure all required API keys and provider endpoints are configured before use.