File size: 1,188 Bytes
428441b |
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 |
# 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):
"""Summarize and synthesize results using OpenAI GPT-4o."""
if not prompt:
prompt = "Summarize these biomedical search results, highlighting key findings and future research directions:"
response = openai.ChatCompletion.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 = ""):
"""Answer a user question using OpenAI GPT-4o, with provided context."""
response = openai.ChatCompletion.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']
|