Skip to content

Commit 225afca

Browse files
committed
0.4.1 release prep
- Add support for multiple workers & CORS headers (`--workers` & `--cors_origin` cmdline option)
1 parent b81d122 commit 225afca

4 files changed

Lines changed: 39 additions & 7 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,16 @@
33
Notable changes to Format based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). Project follows [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
44

55
<!--
6+
## [Unreleased]
7+
68
-->
79

8-
## [Unreleased]
10+
## [0.4.1] - 20240806
911

1012
### Added
1113

1214
- demo `demo/zipcode.py`
15+
- support for multiple workers & CORS headers (`--workers` & `--cors_origin` cmdline option)
1316

1417
### Fixed
1518

demo/arithmetic_calc.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ async def arithmetic_calc(num1=None, num2=None, op=None):
4646

4747
toolio_mm = model_manager(MLX_MODEL_PATH, tool_reg=[arithmetic_calc], trace=True)
4848

49+
# Use this to try parallel function calling
4950
# PROMPT = 'Solve the following calculations: 42 * 42, 24 * 24, 5 * 5, 89 * 75, 42 * 46, 69 * 85, 422 * 420, 753 * 321, 72 * 55, 240 * 204, 789 * 654, 123 * 321, 432 * 89, 564 * 321?' # noqa: E501
5051
PROMPT = 'Solve the following calculation: 4242 * 2424.2'
5152
async def async_main(tmm):

pylib/cli/server.py

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,27 +14,29 @@
1414
Note: you can also point `--model` at a downloaded or converted MLX model on local storage.
1515
'''
1616

17+
import os
1718
import json
1819
import time
19-
import os
2020
from contextlib import asynccontextmanager
2121
import warnings
2222

2323
from fastapi import FastAPI, Request, status
2424
from fastapi.responses import FileResponse, JSONResponse, StreamingResponse
2525
from fastapi.exceptions import RequestValidationError
26+
from fastapi.middleware.cors import CORSMiddleware
2627
import click
2728
import uvicorn
2829

2930
from llm_structured_output.util.output import info, warning, debug
3031

3132
from toolio.schema_helper import Model
3233
from toolio.llm_helper import enrich_chat_for_tools, DEFAULT_FLAGS, FLAGS_LOOKUP
33-
from toolio.http_schematics import V1ChatCompletionsRequest, V1ChatMessage, V1ResponseFormatType
34+
from toolio.http_schematics import V1ChatCompletionsRequest, V1ResponseFormatType
3435
from toolio.responder import (ToolCallStreamingResponder, ToolCallResponder,
3536
ChatCompletionResponder, ChatCompletionStreamingResponder)
3637

3738

39+
NUM_CPUS = int(os.cpu_count())
3840
app_params = {}
3941

4042
# Context manager for the FastAPI app's lifespan: https://fastapi.tiangolo.com/advanced/events/
@@ -57,6 +59,9 @@ async def lifespan(app: FastAPI):
5759
# print(app.state.model.model.__class__, app.state.model.model.model_type)
5860
info(f'Model loaded in {(tdone - tstart)/1000000000.0:.3f}s. Type: {app.state.model.model.model_type}')
5961
app.state.model_flags = FLAGS_LOOKUP.get(app.state.model.model.model_type, DEFAULT_FLAGS)
62+
# Look into exposing control over methods & headers as well
63+
app.add_middleware(CORSMiddleware, allow_origins=app_params['cors_origins'], allow_credentials=True,
64+
allow_methods=["*"], allow_headers=["*"])
6065
yield
6166
# Shutdown code here, if any
6267

@@ -199,7 +204,29 @@ async def post_v1_chat_completions_impl(req_data: V1ChatCompletionsRequest):
199204
help='Path to JSON schema to be used if not provided via API call.'
200205
'Interpolated into {jsonschema} placeholder in prompts')
201206
@click.option('--llmtemp', default='0.1', type=float, help='LLM sampling temperature')
202-
def main(host, port, model, default_schema, default_schema_file, llmtemp):
207+
@click.option('--workers', type=int, default=0,
208+
help='Number of workers processes to spawn (each utilizes one CPU core).'
209+
'Defaults to $WEB_CONCURRENCY environment variable if available, or 1')
210+
@click.option('--cors_origin', multiple=True,
211+
help='Origin to be permitted for CORS https://fastapi.tiangolo.com/tutorial/cors/')
212+
def main(host, port, model, default_schema, default_schema_file, llmtemp, workers, cors_origin):
203213
app_params.update(model=model, default_schema=default_schema, default_schema_fpath=default_schema_file,
204-
llmtemp=llmtemp)
205-
uvicorn.run('toolio.cli.server:app', host=host, port=port, reload=False)
214+
llmtemp=llmtemp, cors_origins=list(cors_origin))
215+
workers = workers or None
216+
# logger.info(f'Host has {NUM_CPUS} CPU cores')
217+
uvicorn.run('toolio.cli.server:app', host=host, port=port, reload=False, workers=workers)
218+
219+
220+
# Implement log config when we
221+
def UNUSED_log_setup(config):
222+
# Set up logging
223+
import logging
224+
global logger # noqa: PLW0603
225+
226+
main_loglevel = config.get('log', {'level': 'INFO'})['level']
227+
logging.config.dictConfig(config['log'])
228+
# Following 2 lines configure the root logger, so all other loggers in this process space will inherit
229+
# logging.basicConfig(level=main_loglevel, format='%(levelname)s:%(name)s: %(message)s')
230+
logging.getLogger().setLevel(main_loglevel) # Seems redundant, but is necessary. Python logging is quirky
231+
logger = logging.getLogger(__name__)
232+
# logger.addFilter(LocalFilter())

pylib/tool/math.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ def calculator(expr=None):
2020
'''
2121
Make an arithmetical, mathematical calculation, using operations such as addition (+), subtraction (-),
2222
multiplication (*), and division (/). Don't forget to use parenthesis for grouping.
23-
**Always use this tool for calculations. Never try to do them yourself**.
23+
**Always use this tool for calculations. Never try to do them yourself. Only use numbers and operators.
24+
Do not include units in numbers!**.
2425
'''
2526
# print(repr(expr))
2627
if not ALLOWED_EXPR_PAT.match(expr):

0 commit comments

Comments
 (0)