forked from containers/ramalama
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Just an initial code, we can always improve on the road. Resolves: containers#725 Signed-off-by: Douglas Schilling Landgraf <dougsland@redhat.com>
- Loading branch information
Showing
4 changed files
with
102 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
__pycache__/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
## Running the server | ||
|
||
In the server side, run the following: | ||
|
||
```console | ||
uvicorn api:app --host 0.0.0.0 --port 8000 --reload | ||
``` | ||
|
||
## Client side getting information | ||
|
||
```console | ||
curl -X GET "http://192.168.82.25:8000/models" | ||
|
||
{"models":["NAME MODIFIED SIZE ","ollama://tinyllama:latest 3 minutes ago 608.16 MB"]}% | ||
``` | ||
|
||
## Posting data | ||
|
||
```console | ||
curl -X POST "http://192.168.82.25:8000/run/tinyllama" & | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
from fastapi import FastAPI | ||
from fastapi.responses import RedirectResponse | ||
import subprocess | ||
|
||
app = FastAPI() | ||
|
||
|
||
# Redirect root "/" to "/docs" | ||
@app.get("/", include_in_schema=False) | ||
def root(): | ||
return RedirectResponse(url="/docs") | ||
|
||
|
||
@app.get("/info") | ||
def info_command(): | ||
try: | ||
result = subprocess.run( | ||
["ramalama", "info"], capture_output=True, text=True | ||
) | ||
return {"info": result.stdout.strip().split("\n")} | ||
except Exception as e: | ||
return {"error": str(e)} | ||
|
||
|
||
@app.get("/ps") | ||
def ps_command(): | ||
try: | ||
result = subprocess.run( | ||
["ramalama", "ps"], capture_output=True, text=True | ||
) | ||
return {"ps": result.stdout.strip().split("\n")} | ||
except Exception as e: | ||
return {"error": str(e)} | ||
|
||
|
||
# List available AI models in RamaLama | ||
@app.get("/models") | ||
def list_models(): | ||
try: | ||
result = subprocess.run( | ||
["ramalama", "list"], capture_output=True, text=True | ||
) | ||
return {"models": result.stdout.strip().split("\n")} | ||
except Exception as e: | ||
return {"error": str(e)} | ||
|
||
|
||
# Pull method | ||
@app.post("/pull/{model_name}") | ||
def pull_model(model_name: str): | ||
try: | ||
command = ["ramalama", "pull", model_name] | ||
subprocess.run(command, check=True) | ||
return {"message": f"Model {model_name} is running"} | ||
except subprocess.CalledProcessError as e: | ||
return {"error": str(e)} | ||
|
||
|
||
# Run an AI model | ||
# (chatbot will open in the server side at this moment) | ||
@app.post("/run/{model_name}") | ||
def run_model(model_name: str): | ||
try: | ||
command = ["ramalama", "run", model_name] | ||
subprocess.run(command, check=True) | ||
return {"message": f"Model {model_name} is running"} | ||
except subprocess.CalledProcessError as e: | ||
return {"error": str(e)} | ||
|
||
|
||
# Stop a running AI model | ||
@app.post("/stop/{model_name}") | ||
def stop_model(model_name: str): | ||
try: | ||
subprocess.run(["ramalama", "stop", model_name], check=True) | ||
return {"message": f"Model {model_name} stopped"} | ||
except Exception as e: | ||
return {"error": str(e)} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
fastapi | ||
uvicorn |
203dcd3
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Project github.com/dougsland/ramalama.git is not on our allowlist! See https://packit.dev/docs/guide/#2-approval