| # mcp/openai_utils.py | |
| import openai | |
| import os | |
| openai.api_key = os.environ.get("OPENAI_API_KEY") | |
| async def ai_summarize(text: str, prompt: str = None): | |
| if not prompt: | |
| prompt = "Summarize these biomedical search results, highlighting key findings and future research directions:" | |
| client = openai.AsyncOpenAI(api_key=openai.api_key) | |
| response = await client.chat.completions.create( | |
| model="gpt-4o", | |
| messages=[ | |
| {"role": "system", "content": "You are an expert biomedical research assistant."}, | |
| {"role": "user", "content": f"{prompt}\n{text}"} | |
| ], | |
| max_tokens=350 | |
| ) | |
| return response.choices[0].message.content | |
| async def ai_qa(question: str, context: str = ""): | |
| client = openai.AsyncOpenAI(api_key=openai.api_key) | |
| response = await client.chat.completions.create( | |
| model="gpt-4o", | |
| messages=[ | |
| {"role": "system", "content": "You are an advanced biomedical research agent."}, | |
| {"role": "user", "content": f"Question: {question}\nContext: {context}"} | |
| ], | |
| max_tokens=350 | |
| ) | |
| return response.choices[0].message.content | |