Skip to content

Commit

Permalink
reorganize and add new services for propmt
Browse files Browse the repository at this point in the history
  • Loading branch information
mmabrouk committed Dec 14, 2024
1 parent ce9a740 commit f3e04b1
Show file tree
Hide file tree
Showing 39 changed files with 866 additions and 52 deletions.
22 changes: 16 additions & 6 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -214,15 +214,25 @@ services:
file: ./services/completion-stateless-sdk/docker-compose.yml
service: completion-stateless-sdk

completion-live-sdk:
completion-new-sdk:
extends:
file: ./services/completion-live-sdk/docker-compose.yml
service: completion-live-sdk
file: ./services/completion-new-sdk/docker-compose.yml
service: completion-new-sdk

chat-live-sdk:
chat-new-sdk:
extends:
file: ./services/chat-live-sdk/docker-compose.yml
service: chat-live-sdk
file: ./services/chat-new-sdk/docker-compose.yml
service: chat-new-sdk

completion-new-sdk-prompt:
extends:
file: ./services/completion-new-sdk-prompt/docker-compose.yml
service: completion-new-sdk-prompt

chat-new-sdk-prompt:
extends:
file: ./services/chat-new-sdk-prompt/docker-compose.yml
service: chat-new-sdk-prompt

networks:
agenta-network:
Expand Down
33 changes: 33 additions & 0 deletions services/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Agenta Services

This directory contains various versions of Agenta's LLM services, each offering distinct capabilities and interfaces for language model interactions.

## Service Overview

### Legacy Services
- **completion-old-sdk**: Original completion service (as in current release)
- **chat-old-sdk**: Original chat service (as in current release)

### New Services
All services with "new-sdk" utilize the modified SDK, which includes these changes:
- Configuration is now nested under `agenta_config` in the request body (no longer flattened)
- Implements the stateless SDK (no interface changes, but may introduce future issues in cloud deployment due to lack of testing)

We've created two versions of each new service:
1. Original logic with new SDK:
- completion-new-sdk
- chat-new-sdk
2. New prompt object and updated logic:
- completion-new-sdk-prompt
- chat-new-sdk-prompt

## Service Components

Each service includes:
- Docker configuration (`docker-compose.yml`)
- REST API documentation (`.rest` files)
- Implementation code (`_app.py`)

## Usage

For usage examples and API details, refer to the `.rest` files in each service's directory.
23 changes: 0 additions & 23 deletions services/chat-live-sdk/docker-compose.yml

This file was deleted.

33 changes: 33 additions & 0 deletions services/chat-new-sdk-prompt.rest
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
### Test chat-new-sdk-prompt
POST http://localhost/chat-new-sdk-prompt/chat
Content-Type: application/json

{
"inputs": {
"message": "What is the capital of France?"
}
}

### Test chat configuration with prompt
POST http://localhost/chat-new-sdk-prompt/configure
Content-Type: application/json

{
"model": "gpt-3.5-turbo",
"temperature": 0.7,
"max_tokens": 100,
"prompt": {
"messages": [
{
"role": "system",
"content": "You are a helpful assistant specializing in geography."
}
],
"template_format": "fstring",
"response_format": {
"type": "text"
},
"tools": null,
"tool_choice": "auto"
}
}
File renamed without changes.
File renamed without changes.
23 changes: 23 additions & 0 deletions services/chat-new-sdk-prompt/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
services:
chat-new-sdk-prompt:
build: .
volumes:
- .:/app
- ../../agenta-cli:/agenta-cli
environment:
- AGENTA_UNAUTHORIZED_EXECUTION_ALLOWED=True
- AGENTA_HOST=http://host.docker.internal
networks:
- agenta-network
labels:
- "traefik.http.routers.chat-new-sdk-prompt.rule=PathPrefix(`/chat-new-sdk-prompt/`)"
- "traefik.http.routers.chat-new-sdk-prompt.entrypoints=web"
- "traefik.http.middlewares.chat-new-sdk-prompt-strip.stripprefix.prefixes=/chat-new-sdk-prompt"
- "traefik.http.middlewares.chat-new-sdk-prompt-strip.stripprefix.forceslash=true"
- "traefik.http.routers.chat-new-sdk-prompt.middlewares=chat-new-sdk-prompt-strip"
- "traefik.http.services.chat-new-sdk-prompt.loadbalancer.server.port=80"
- "traefik.http.routers.chat-new-sdk-prompt.service=chat-new-sdk-prompt"

networks:
agenta-network:
external: true
File renamed without changes.
File renamed without changes.
File renamed without changes.
29 changes: 29 additions & 0 deletions services/chat-new-sdk.rest
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,35 @@
### Health Check
GET {{baseUrl}}/{{service}}/health HTTP/1.1

### Test chat-new-sdk
POST {{baseUrl}}/{{service}}/chat HTTP/1.1
Content-Type: application/json

{
"inputs": {
"message": "What is the capital of France?"
}
}

### Test chat configuration
POST {{baseUrl}}/{{service}}/configure HTTP/1.1
Content-Type: application/json

{
"model": "gpt-3.5-turbo",
"temperature": 0.7,
"max_tokens": 100,
"prompt": {
"messages": [
{
"role": "system",
"content": "You are a helpful assistant specializing in geography."
}
],
"template_format": "fstring"
}
}

### Generate Chat Response
POST {{baseUrl}}/{{service}}/generate HTTP/1.1
Content-Type: application/json
Expand Down
File renamed without changes.
File renamed without changes.
65 changes: 65 additions & 0 deletions services/chat-new-sdk/_app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
from typing import Annotated, Any, Dict, List

import agenta as ag
from agenta.sdk.assets import supported_llm_models
from pydantic import BaseModel, Field
import os
# Import mock if MOCK_LLM environment variable is set
if os.getenv("MOCK_LLM", True):
from mock_litellm import MockLiteLLM

litellm = MockLiteLLM()
else:
import litellm

litellm.drop_params = True
litellm.callbacks = [ag.callbacks.litellm_handler()]

SYSTEM_PROMPT = "You have expertise in offering technical ideas to startups."

ag.init()


class MyConfig(BaseModel):
temperature: float = Field(default=0.2, le=1, ge=0)
model: Annotated[str, ag.MultipleChoice(choices=supported_llm_models)] = Field(
default="gpt-3.5-turbo"
)
max_tokens: int = Field(default=-1, ge=-1, le=4000)
prompt_system: str = Field(default=SYSTEM_PROMPT)


@ag.instrument(spankind="llm")
async def llm_call(messages: List[Dict[str, Any]], maxtokens):
config = ag.ConfigManager.get_from_route(schema=MyConfig)
chat_completion = await litellm.acompletion(
model=config.model,
messages=messages,
temperature=config.temperature,
max_tokens=maxtokens,
)
token_usage = chat_completion.usage.dict()
return {
"usage": token_usage,
"message": chat_completion.choices[0].message.content,
"cost": litellm.cost_calculator.completion_cost(
completion_response=chat_completion, model=config.model
),
}


@ag.route("/", config_schema=MyConfig)
@ag.instrument()
async def chat(inputs: ag.MessagesInput = ag.MessagesInput()) -> Dict[str, Any]:
config = ag.ConfigManager.get_from_route(schema=MyConfig)
messages = [{"role": "system", "content": config.prompt_system}] + inputs
max_tokens = config.max_tokens if config.max_tokens != -1 else None
response = await llm_call(
messages=messages,
maxtokens=max_tokens,
)
return {
"message": response["message"],
"usage": response.get("usage", None),
"cost": response.get("cost", None),
}
23 changes: 23 additions & 0 deletions services/chat-new-sdk/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
services:
chat-new-sdk:
build: .
volumes:
- .:/app
- ../../agenta-cli:/agenta-cli
environment:
- AGENTA_UNAUTHORIZED_EXECUTION_ALLOWED=True
- AGENTA_HOST=http://host.docker.internal
networks:
- agenta-network
labels:
- "traefik.http.routers.chat-new-sdk.rule=PathPrefix(`/chat-new-sdk/`)"
- "traefik.http.routers.chat-new-sdk.entrypoints=web"
- "traefik.http.middlewares.chat-new-sdk-strip.stripprefix.prefixes=/chat-new-sdk"
- "traefik.http.middlewares.chat-new-sdk-strip.stripprefix.forceslash=true"
- "traefik.http.routers.chat-new-sdk.middlewares=chat-new-sdk-strip"
- "traefik.http.services.chat-new-sdk.loadbalancer.server.port=80"
- "traefik.http.routers.chat-new-sdk.service=chat-new-sdk"

networks:
agenta-network:
external: true
File renamed without changes.
File renamed without changes.
File renamed without changes.
23 changes: 0 additions & 23 deletions services/completion-live-sdk/docker-compose.yml

This file was deleted.

37 changes: 37 additions & 0 deletions services/completion-new-sdk-prompt.rest
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
### Test completion-new-sdk-prompt
POST http://localhost/completion-new-sdk-prompt/generate
Content-Type: application/json

{
"inputs": {
"country": "France"
}
}

### Test completion configuration with prompt
POST http://localhost/completion-new-sdk-prompt/configure
Content-Type: application/json

{
"model": "gpt-3.5-turbo",
"temperature": 0.7,
"max_tokens": 100,
"prompt": {
"messages": [
{
"role": "system",
"content": "You are a helpful assistant."
},
{
"role": "user",
"content": "What is the capital of {country}?"
}
],
"template_format": "fstring",
"response_format": {
"type": "text"
},
"tools": null,
"tool_choice": "auto"
}
}
18 changes: 18 additions & 0 deletions services/completion-new-sdk-prompt/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
FROM python:3.10-slim

ARG ROOT_PATH=/
ENV ROOT_PATH=${ROOT_PATH}

WORKDIR /app

COPY . .

RUN pip install --upgrade pip \
&& pip install --no-cache-dir agenta openai python-dotenv uvicorn "litellm>=1.0,<2.0" google-generativeai

# Add agenta-cli to PYTHONPATH so it can find the local agenta package
ENV PYTHONPATH=/agenta-cli:$PYTHONPATH

EXPOSE 80

CMD ["./entrypoint.sh"]
16 changes: 16 additions & 0 deletions services/completion-new-sdk-prompt/Dockerfile.prerelease
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
FROM python:3.10-slim

ARG ROOT_PATH=/
ENV ROOT_PATH=${ROOT_PATH}

WORKDIR /app

COPY . .

RUN pip install --upgrade pip \
&& pip install --no-cache-dir openai python-dotenv uvicorn "litellm>=1.0,<2.0" google-generativeai \
&& pip install --no-cache-dir --pre agenta

EXPOSE 80

CMD ["./entrypoint.sh"]
Loading

0 comments on commit f3e04b1

Please sign in to comment.