Skip to content

Commit

Permalink
poetry config
Browse files Browse the repository at this point in the history
- setup poetry to use a local venv
- add that venv to node-calls-python
- simple dependency import test for sig-gen
  • Loading branch information
josephjclark committed Apr 24, 2024
1 parent 4272153 commit b180554
Show file tree
Hide file tree
Showing 18 changed files with 2,318 additions and 28 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ tmp
# Poetry specific files and directories
.poetry/
__pypackages__/
.venv

# node.js stuff
node_modules
Expand Down
File renamed without changes.
7 changes: 7 additions & 0 deletions platform/src/bridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ const py = nodecallspython.interpreter;
// (and maybe preload some core stuff)
export const run = async (scriptName: string, fnName: string, args: JSON) => {
try {
// poetry should be configured to use a vnv in the local filesystem
// This makes it really easy to tell node-calls-python about the right env!
// TODO: Ah,not THAT easy, we need to work out the python version
py.addImportPath(path.resolve(".venv/lib/python3.11/site-packages"));

// TODO in dev mode I want to re-import the module every time
// But in prod I wanna use the cached import
const pymodule = await py.import(
path.resolve(`./services/${scriptName}/${scriptName}.py`),
true
Expand Down
3 changes: 2 additions & 1 deletion platform/src/middleware/services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ import { run } from "../bridge";
import describeModules from "../util/describe-modules";

export default async (app: Elysia) => {
console.log("Loading routes:");
const modules = await describeModules(path.resolve("./services"));

app.group("/services", (app) => {
modules.forEach(({ name }) => {
console.log(" - mounted /services/" + name);
app.post(name, async (ctx) => {
// Note that elysia handles json parsing for me - neat!
const payload = ctx.body;
Expand Down
220 changes: 218 additions & 2 deletions poetry.lock

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions poetry.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# set up a .venv in this project
virtualenvs.in-project = true
50 changes: 26 additions & 24 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
[tool.poetry]
name = "code-generator"
name = "apollo"
version = "0.1.0"
description = "A python monorepo for AI code generation"
authors = ["Isma-Ilou Sadou <[email protected]>"]
license = "LGPLv3"
readme = "README.md"
package-mode = false

[tool.poetry.dependencies]
python = "^3.9"
requests = "^2.31.0"
fastapi = "^0.110.2"


[build-system]
Expand All @@ -20,34 +22,34 @@ line-length = 120

[tool.ruff]
select = [
"E", # pycodestyle
"F", # pyflakes
"W", # pycodestyle (warnings)
"I", # isort
"N", # pep8-naming
"UP", # pyupgrade
"ANN", # flake8-annotations
"B", # flake8-bugbear
"A", # flake8-builtins
"COM", # flake8-commas
"C4", # flake8-comprehensions
"DTZ", # flake8-datetimez
"EXE", # flake8-executable
"PIE", # flake8-pie
"T20", # flake8-print
"PT", # flake8-pytest
"SIM", # flake8-simplify
"ARG", # flake8-unused-arguments
"PTH", # flake8--use-pathlib
"ERA", # flake8-eradicate
"RUF", # ruff specific rules
"PL", # pylint
"E", # pycodestyle
"F", # pyflakes
"W", # pycodestyle (warnings)
"I", # isort
"N", # pep8-naming
"UP", # pyupgrade
"ANN", # flake8-annotations
"B", # flake8-bugbear
"A", # flake8-builtins
"COM", # flake8-commas
"C4", # flake8-comprehensions
"DTZ", # flake8-datetimez
"EXE", # flake8-executable
"PIE", # flake8-pie
"T20", # flake8-print
"PT", # flake8-pytest
"SIM", # flake8-simplify
"ARG", # flake8-unused-arguments
"PTH", # flake8--use-pathlib
"ERA", # flake8-eradicate
"RUF", # ruff specific rules
"PL", # pylint
]
ignore = [
"ANN101", # Missing type annotation for self in method
"ANN204", # Missing type annotation for special method
"ANN003", # Missing type annotation for `**kwargs`
"UP007", # Use `X | Y` for type annotations instead of Union
"UP007", # Use `X | Y` for type annotations instead of Union
"E501",
]
line-length = 120
Expand Down
4 changes: 3 additions & 1 deletion services/echo/echo.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from log import log

# Simple python service to echo requests back to the caller
# Used in test
def main(x):
print('Echoing request')
log()
return x
2 changes: 2 additions & 0 deletions services/echo/log.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
def log():
print('Echoing request')
Empty file.
9 changes: 9 additions & 0 deletions services/signature_generator/constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# API Constants
HOST = "0.0.0.0"
PORT = 8001

# Inference Endpoints
# CODET5_ENDPOINT = "http://localhost:8002/codet5/generate_code/"

# codes
SUCCESS_CODE: int = 200
Binary file not shown.
39 changes: 39 additions & 0 deletions services/signature_generator/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import logging
from typing import Union
from fastapi import HTTPException

import requests
from pydantic import BaseModel

logging.basicConfig(level=logging.INFO)

# TODO - remove all the HTTP stuff from this
class SignatureGenerator:
def __init__(self, endpoint_url: str):
self.endpoint_url = endpoint_url
self.logger = logging.getLogger(__name__)

def generate(self, prompt: str) -> str:
try:
headers = {"Content-Type": "application/json"}
data = {"prompt": prompt}

response = requests.post(self.endpoint_url, headers=headers, json=data)
response.raise_for_status()
return response.json().get("generated_code")
except requests.exceptions.HTTPError as e:
error_message = f"HTTP error occurred: {e.response.status_code}, {e.response.text}, {self.endpoint_url}"
self.logger.error(error_message)
raise HTTPException(
status_code=e.response.status_code, detail=error_message
) from e
except Exception as e:
error_message = f"An unexpected error occurred in code gen: {e}"
self.logger.error(error_message)
raise HTTPException(status_code=500, detail=error_message)


class SignatureInput(BaseModel):
open_api_spec: Union[str, dict]
instruction: str
model: str = "codet5"
Loading

0 comments on commit b180554

Please sign in to comment.