Create openai_utils.py
Browse files- mcp/openai_utils.py +32 -0
mcp/openai_utils.py
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# mcp/openai_utils.py
|
| 2 |
+
|
| 3 |
+
import openai
|
| 4 |
+
import os
|
| 5 |
+
|
| 6 |
+
openai.api_key = os.environ.get("OPENAI_API_KEY")
|
| 7 |
+
|
| 8 |
+
async def ai_summarize(text: str, prompt: str = None):
|
| 9 |
+
"""Summarize and synthesize results using OpenAI GPT-4o."""
|
| 10 |
+
if not prompt:
|
| 11 |
+
prompt = "Summarize these biomedical search results, highlighting key findings and future research directions:"
|
| 12 |
+
response = openai.ChatCompletion.create(
|
| 13 |
+
model="gpt-4o",
|
| 14 |
+
messages=[
|
| 15 |
+
{"role": "system", "content": "You are an expert biomedical research assistant."},
|
| 16 |
+
{"role": "user", "content": f"{prompt}\n{text}"}
|
| 17 |
+
],
|
| 18 |
+
max_tokens=350
|
| 19 |
+
)
|
| 20 |
+
return response['choices'][0]['message']['content']
|
| 21 |
+
|
| 22 |
+
async def ai_qa(question: str, context: str = ""):
|
| 23 |
+
"""Answer a user question using OpenAI GPT-4o, with provided context."""
|
| 24 |
+
response = openai.ChatCompletion.create(
|
| 25 |
+
model="gpt-4o",
|
| 26 |
+
messages=[
|
| 27 |
+
{"role": "system", "content": "You are an advanced biomedical research agent."},
|
| 28 |
+
{"role": "user", "content": f"Question: {question}\nContext: {context}"}
|
| 29 |
+
],
|
| 30 |
+
max_tokens=350
|
| 31 |
+
)
|
| 32 |
+
return response['choices'][0]['message']['content']
|