infernet-1.0.0 update

This commit is contained in:
arshan-ritual
2024-06-06 13:18:48 -04:00
parent 2a11fd3953
commit 40a6c590da
98 changed files with 879 additions and 506 deletions

View File

@ -7,12 +7,15 @@ ENV PYTHONDONTWRITEBYTECODE 1
ENV PIP_NO_CACHE_DIR 1
ENV RUNTIME docker
ENV PYTHONPATH src
ARG index_url
ENV UV_EXTRA_INDEX_URL ${index_url}
RUN apt-get update
RUN apt-get install -y git curl
# install uv
ADD --chmod=755 https://astral.sh/uv/install.sh /install.sh
ADD https://astral.sh/uv/install.sh /install.sh
RUN chmod 755 /install.sh
RUN /install.sh && rm /install.sh
COPY src/requirements.txt .

View File

@ -5,8 +5,7 @@ TAG := $(DOCKER_ORG)/example-$(EXAMPLE_NAME)-infernet:latest
.phony: build run build-multiplatform try-prompt
build:
mkdir -p root-config
@docker build -t $(TAG) .
@docker build -t $(TAG) --build-arg index_url=$(index_url) .
run: build
@docker run --env-file $(EXAMPLE_NAME).env -p 3000:3000 $(TAG)

View File

@ -16,5 +16,5 @@ make run
## Test the Container
```bash
curl -X POST localhost:3000/service_output -H "Content-Type: application/json" \
-d '{"source": 1, "data": {"text": "can shrimps actually fry rice?"}}'
-d '{"source": 1, "data": {"prompt": "can shrimps actually fry rice?"}}'
```

View File

@ -7,7 +7,7 @@
"enabled": true,
"trail_head_blocks": 0,
"rpc_url": "http://host.docker.internal:8545",
"coordinator_address": "0x5FbDB2315678afecb367f032d93F642f64180aa3",
"registry_address": "0x663F3ad617193148711d28f5334eE4Ed07016602",
"wallet": {
"max_gas_limit": 4000000,
"private_key": "0x59c6995e998f97a5a0044966f0945389dc9e86dae88c7a8412f4603b6b78690d"
@ -23,6 +23,10 @@
"port": 6379
},
"forward_stats": true,
"snapshot_sync": {
"sleep": 3,
"batch_size": 100
},
"containers": [
{
"id": "gpt4",
@ -34,19 +38,9 @@
"allowed_ips": [],
"command": "--bind=0.0.0.0:3000 --workers=2",
"env": {
"OPENAI_API_KEY": "barabeem baraboom"
}
},
{
"id": "anvil-node",
"image": "ritualnetwork/infernet-anvil:0.0.0",
"external": true,
"port": "8545",
"allowed_delegate_addresses": [],
"allowed_addresses": [],
"allowed_ips": [],
"command": "",
"env": {}
"OPENAI_API_KEY": "your-key"
},
"accepted_payments": {}
}
]
}

View File

@ -1,8 +1,16 @@
import logging
import os
from typing import Any, cast
from eth_abi import decode, encode # type: ignore
from infernet_ml.utils.service_models import InfernetInput, InfernetInputSource
from infernet_ml.utils.css_mux import (
ConvoMessage,
CSSCompletionParams,
CSSRequest,
Provider,
)
from infernet_ml.utils.service_models import InfernetInput
from infernet_ml.utils.service_models import JobLocation
from infernet_ml.workflows.inference.css_inference_workflow import CSSInferenceWorkflow
from quart import Quart, request
@ -12,7 +20,9 @@ log = logging.getLogger(__name__)
def create_app() -> Quart:
app = Quart(__name__)
workflow = CSSInferenceWorkflow(provider="OPENAI", endpoint="completions")
workflow = CSSInferenceWorkflow(
api_keys={Provider.OPENAI: os.environ["OPENAI_API_KEY"]}
)
workflow.setup()
@ -24,7 +34,7 @@ def create_app() -> Quart:
return "GPT4 Example Program"
@app.route("/service_output", methods=["POST"])
async def inference() -> dict[str, Any]:
async def inference() -> Any:
req_data = await request.get_json()
"""
InfernetInput has the format:
@ -33,52 +43,62 @@ def create_app() -> Quart:
"""
infernet_input: InfernetInput = InfernetInput(**req_data)
if infernet_input.source == InfernetInputSource.OFFCHAIN:
prompt = cast(dict[str, Any], infernet_input.data).get("prompt")
else:
# On-chain requests are sent as a generalized hex-string which we will
# decode to the appropriate format.
(prompt,) = decode(
["string"], bytes.fromhex(cast(str, infernet_input.data))
)
match infernet_input:
case InfernetInput(source=JobLocation.OFFCHAIN):
prompt = cast(dict[str, Any], infernet_input.data).get("prompt")
case InfernetInput(source=JobLocation.ONCHAIN):
# On-chain requests are sent as a generalized hex-string which we will
# decode to the appropriate format.
(prompt,) = decode(
["string"], bytes.fromhex(cast(str, infernet_input.data))
)
case _:
raise ValueError("Invalid source")
result: dict[str, Any] = workflow.inference(
{
"model": "gpt-4-0613",
"params": {
"endpoint": "completions",
"messages": [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": prompt},
],
},
}
result = workflow.inference(
CSSRequest(
provider=Provider.OPENAI,
endpoint="completions",
model="gpt-4-0613",
params=CSSCompletionParams(
messages=[
ConvoMessage(
role="system", content="you are a helpful " "assistant."
),
ConvoMessage(role="user", content=cast(str, prompt)),
]
),
)
)
if infernet_input.source == InfernetInputSource.OFFCHAIN:
"""
In case of an off-chain request, the result is returned as is.
"""
return {"message": result}
else:
"""
In case of an on-chain request, the result is returned in the format:
{
"raw_input": str,
"processed_input": str,
"raw_output": str,
"processed_output": str,
"proof": str,
}
refer to: https://docs.ritual.net/infernet/node/containers for more info.
"""
return {
"raw_input": "",
"processed_input": "",
"raw_output": encode(["string"], [result]).hex(),
"processed_output": "",
"proof": "",
}
match infernet_input:
case InfernetInput(destination=JobLocation.OFFCHAIN):
"""
In case of an off-chain request, the result is returned as is.
"""
return {"message": result}
case InfernetInput(destination=JobLocation.ONCHAIN):
"""
In case of an on-chain request, the result is returned in the format:
{
"raw_input": str,
"processed_input": str,
"raw_output": str,
"processed_output": str,
"proof": str,
}
refer to: https://docs.ritual.net/infernet/node/containers for more
info.
"""
return {
"raw_input": "",
"processed_input": "",
"raw_output": encode(["string"], [result]).hex(),
"processed_output": "",
"proof": "",
}
case _:
raise ValueError("Invalid destination")
return app

View File

@ -1,5 +1,4 @@
quart==0.19.4
infernet_ml==0.1.0
PyArweave @ git+https://github.com/ritual-net/pyarweave.git
infernet-ml==1.0.0
infernet-ml[css_inference]==1.0.0
web3==6.15.0
retry2==0.9.5