|
| 1 | +# Function |
| 2 | +import logging |
| 3 | +from ollama import Client |
| 4 | +import json |
| 5 | +import os |
| 6 | + |
| 7 | +def new(): |
| 8 | + """ New is the only method that must be implemented by a Function. |
| 9 | + The instance returned can be of any name. |
| 10 | + """ |
| 11 | + return Function() |
| 12 | + |
| 13 | +# helper function for sending responses |
| 14 | +async def send_it(send,msg:str|None): |
| 15 | + if msg == None: |
| 16 | + msg = "" |
| 17 | + |
| 18 | + await send({ |
| 19 | + 'type': 'http.response.start', |
| 20 | + 'status': 200, |
| 21 | + 'headers': [ |
| 22 | + [b'content-type', b'text/plain'], |
| 23 | + ], |
| 24 | + }) |
| 25 | + await send({ |
| 26 | + 'type': 'http.response.body', |
| 27 | + 'body': msg.encode(), |
| 28 | + }) |
| 29 | + |
| 30 | +class Function: |
| 31 | + def __init__(self): |
| 32 | + """ The init method is an optional method where initialization can be |
| 33 | + performed. See the start method for a startup hook which includes |
| 34 | + configuration. |
| 35 | + """ |
| 36 | + self.client = Client( |
| 37 | + # where your OLLAMA server is running |
| 38 | + host=os.environ.get("OLLAMA_HOST","127.0.0.1:11434") |
| 39 | + ) |
| 40 | + |
| 41 | + async def handle(self, scope, receive, send): |
| 42 | + """ Handle all HTTP requests to this Function other than readiness |
| 43 | + and liveness probes. |
| 44 | +
|
| 45 | + To communicate with the LLM following curl data is expected: |
| 46 | + { |
| 47 | + "prompt":"Your prompt for LLM", |
| 48 | + "model": "Your preffered ollama-compatible model", |
| 49 | + } |
| 50 | +
|
| 51 | + Note: Both of these have defaults, therefore you dont need to |
| 52 | + provide them. |
| 53 | +
|
| 54 | + example: curl <host:port> -d '{"prompt":"What is philosophy exactly"}' |
| 55 | + """ |
| 56 | + logging.info("OK: Request Received") |
| 57 | + |
| 58 | + if scope["method"] == "GET": |
| 59 | + await send_it(send,'OK') |
| 60 | + return |
| 61 | + |
| 62 | + # 1) extract the whole body from request |
| 63 | + body = b'' |
| 64 | + more_body = True |
| 65 | + while more_body: |
| 66 | + message = await receive() |
| 67 | + body += message.get('body', b'') |
| 68 | + more_body = message.get('more_body', False) |
| 69 | + |
| 70 | + # 2) decode the request and fetch info |
| 71 | + data = json.loads(body.decode('utf-8')) |
| 72 | + prompt = data.get('prompt','Who are you?') |
| 73 | + model = data.get('model',"llama3.2:1b") |
| 74 | + |
| 75 | + print(f"using model {model}") |
| 76 | + # 3) make /api/chat request to the ollama server |
| 77 | + response = self.client.chat( |
| 78 | + # assign your model here |
| 79 | + model=model, |
| 80 | + messages=[ |
| 81 | + { |
| 82 | + 'role':'user', |
| 83 | + 'content':prompt, |
| 84 | + }, |
| 85 | + ]) |
| 86 | + |
| 87 | + # 4) return the response to the calling client |
| 88 | + await send_it(send,response.message.content) |
| 89 | + |
| 90 | + def start(self, cfg): |
| 91 | + """ start is an optional method which is called when a new Function |
| 92 | + instance is started, such as when scaling up or during an update. |
| 93 | + Provided is a dictionary containing all environmental configuration. |
| 94 | + Args: |
| 95 | + cfg (Dict[str, str]): A dictionary containing environmental config. |
| 96 | + In most cases this will be a copy of os.environ, but it is |
| 97 | + best practice to use this cfg dict instead of os.environ. |
| 98 | + """ |
| 99 | + logging.info("Function starting") |
| 100 | + |
| 101 | + def stop(self): |
| 102 | + """ stop is an optional method which is called when a function is |
| 103 | + stopped, such as when scaled down, updated, or manually canceled. Stop |
| 104 | + can block while performing function shutdown/cleanup operations. The |
| 105 | + process will eventually be killed if this method blocks beyond the |
| 106 | + platform's configured maximum studown timeout. |
| 107 | + """ |
| 108 | + logging.info("Function stopping") |
| 109 | + |
| 110 | + def alive(self): |
| 111 | + """ alive is an optional method for performing a deep check on your |
| 112 | + Function's liveness. If removed, the system will assume the function |
| 113 | + is ready if the process is running. This is exposed by default at the |
| 114 | + path /health/liveness. The optional string return is a message. |
| 115 | + """ |
| 116 | + return True, "Alive" |
| 117 | + |
| 118 | + def ready(self): |
| 119 | + """ ready is an optional method for performing a deep check on your |
| 120 | + Function's readiness. If removed, the system will assume the function |
| 121 | + is ready if the process is running. This is exposed by default at the |
| 122 | + path /health/rediness. |
| 123 | + """ |
| 124 | + return True, "Ready" |
0 commit comments