YussefGAFeer commited on
Commit
939ca52
·
verified ·
1 Parent(s): b18071e

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +73 -0
app.py ADDED
@@ -0,0 +1,73 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, HTTPException
2
+ from fastapi.middleware.cors import CORSMiddleware
3
+ import httpx
4
+ import os
5
+
6
+ app = FastAPI(title="Ollama with MCP Tools")
7
+
8
+ # CORS middleware
9
+ app.add_middleware(
10
+ CORSMiddleware,
11
+ allow_origins=["*"],
12
+ allow_credentials=True,
13
+ allow_methods=["*"],
14
+ allow_headers=["*"],
15
+ )
16
+
17
+ # Ollama API configuration
18
+ OLLAMA_API_URL = "http://localhost:11434/api"
19
+
20
+ # MCP Tools integration
21
+ class MCPTools:
22
+ @staticmethod
23
+ async def process_with_mcp(prompt: str):
24
+ # Example MCP tool integration
25
+ # Replace with actual MCP tool implementation
26
+ return {"status": "processed_with_mcp", "result": f"Processed: {prompt}"}
27
+
28
+ # Ollama API client
29
+ class OllamaClient:
30
+ @staticmethod
31
+ async def generate(prompt: str, model: str = "gemma3n:2b"):
32
+ async with httpx.AsyncClient() as client:
33
+ try:
34
+ response = await client.post(
35
+ f"{OLLAMA_API_URL}/generate",
36
+ json={
37
+ "model": model,
38
+ "prompt": prompt,
39
+ "stream": False
40
+ },
41
+ timeout=60.0
42
+ )
43
+ return response.json()
44
+ except Exception as e:
45
+ raise HTTPException(status_code=500, detail=str(e))
46
+
47
+ # API Endpoints
48
+ @app.get("/")
49
+ async def read_root():
50
+ return {"status": "Ollama API with MCP Tools is running"}
51
+
52
+ @app.post("/api/generate")
53
+ async def generate_text(prompt: str, use_mcp: bool = False):
54
+ if use_mcp:
55
+ mcp_result = await MCPTools.process_with_mcp(prompt)
56
+ return mcp_result
57
+
58
+ ollama_response = await OllamaClient().generate(prompt)
59
+ return {"response": ollama_response}
60
+
61
+ # Health check endpoint
62
+ @app.get("/health")
63
+ async def health_check():
64
+ try:
65
+ async with httpx.AsyncClient() as client:
66
+ response = await client.get(f"{OLLAMA_API_URL}/tags")
67
+ return {"status": "healthy", "ollama_status": "running" if response.status_code == 200 else "error"}
68
+ except Exception as e:
69
+ return {"status": "error", "message": str(e)}
70
+
71
+ if __name__ == "__main__":
72
+ import uvicorn
73
+ uvicorn.run(app, host="0.0.0.0", port=8000)