mirror of
https://github.com/katanemo/plano.git
synced 2026-04-25 16:56:24 +02:00
refactor demos (#398)
This commit is contained in:
parent
2bd61d628c
commit
b3c95a6698
93 changed files with 338 additions and 1042 deletions
18
demos/samples_java/weather_forcecast_service/Dockerfile
Normal file
18
demos/samples_java/weather_forcecast_service/Dockerfile
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
# Stage 1: Build the application using Maven
|
||||
FROM maven:3.8.7-openjdk-18-slim AS build
|
||||
WORKDIR /app
|
||||
# Copy pom.xml and download dependencies first (caching)
|
||||
COPY pom.xml .
|
||||
RUN mvn dependency:go-offline
|
||||
# Copy the source code and build the application
|
||||
COPY src ./src
|
||||
RUN mvn clean package -DskipTests
|
||||
|
||||
# Stage 2: Run the application using a slim JDK image
|
||||
FROM openjdk:17-jdk-slim
|
||||
WORKDIR /app
|
||||
# Copy the built jar from the previous stage
|
||||
COPY --from=build /app/target/weather-forecast-service-0.0.1-SNAPSHOT.jar app.jar
|
||||
# Expose the port on which the app runs (default Spring Boot is 8080)
|
||||
EXPOSE 8081
|
||||
ENTRYPOINT ["java", "-jar", "app.jar"]
|
||||
|
|
@ -0,0 +1,45 @@
|
|||
version: v0.1
|
||||
listener:
|
||||
address: 127.0.0.1
|
||||
port: 10000 #If you configure port 443, you'll need to update the listener with tls_certificates
|
||||
message_format: huggingface
|
||||
|
||||
# Centralized way to manage LLMs, manage keys, retry logic, failover and limits in a central way
|
||||
llm_providers:
|
||||
- name: OpenAI
|
||||
provider_interface: openai
|
||||
access_key: $OPENAI_API_KEY
|
||||
model: gpt-4o-mini
|
||||
default: true
|
||||
|
||||
# Arch creates a round-robin load balancing between different endpoints, managed via the cluster subsystem.
|
||||
endpoints:
|
||||
weather_forecast_service:
|
||||
# value could be ip address or a hostname with port
|
||||
# this could also be a list of endpoints for load balancing
|
||||
# for example endpoint: [ ip1:port, ip2:port ]
|
||||
endpoint: host.docker.internal:18081
|
||||
# max time to wait for a connection to be established
|
||||
connect_timeout: 0.005s
|
||||
|
||||
# default system prompt used by all prompt targets
|
||||
system_prompt: |
|
||||
You are a helpful weather assistant.
|
||||
|
||||
prompt_targets:
|
||||
- name: weather_forecast
|
||||
description: get the weather forecast
|
||||
parameters:
|
||||
- name: location
|
||||
description: the location for which to get the weather forecast
|
||||
required: true
|
||||
type: string
|
||||
format: City, State
|
||||
- name: days
|
||||
description: the number of days for the forecast
|
||||
required: true
|
||||
type: int
|
||||
endpoint:
|
||||
name: weather_forecast_service
|
||||
path: /weather
|
||||
http_method: POST
|
||||
|
|
@ -0,0 +1,20 @@
|
|||
services:
|
||||
weather_forecast_service:
|
||||
build:
|
||||
context: .
|
||||
dockerfile: Dockerfile
|
||||
ports:
|
||||
- "18081:8081"
|
||||
|
||||
chatbot_ui:
|
||||
build:
|
||||
context: ../../shared/chatbot_ui
|
||||
dockerfile: Dockerfile
|
||||
ports:
|
||||
- "18080:8080"
|
||||
environment:
|
||||
- CHAT_COMPLETION_ENDPOINT=http://host.docker.internal:10000/v1
|
||||
extra_hosts:
|
||||
- "host.docker.internal:host-gateway"
|
||||
volumes:
|
||||
- ./arch_config.yaml:/app/arch_config.yaml
|
||||
40
demos/samples_java/weather_forcecast_service/pom.xml
Normal file
40
demos/samples_java/weather_forcecast_service/pom.xml
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
|
||||
https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>weather</groupId>
|
||||
<artifactId>weather-forecast-service</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>2.7.10</version>
|
||||
<relativePath/>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<!-- Spring Boot Starter Web -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<!-- Spring Boot Maven Plugin -->
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
||||
47
demos/samples_java/weather_forcecast_service/run_demo.sh
Normal file
47
demos/samples_java/weather_forcecast_service/run_demo.sh
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
#!/bin/bash
|
||||
set -e
|
||||
|
||||
# Function to start the demo
|
||||
start_demo() {
|
||||
# Step 1: Check if .env file exists
|
||||
if [ -f ".env" ]; then
|
||||
echo ".env file already exists. Skipping creation."
|
||||
else
|
||||
# Step 2: Create `.env` file and set OpenAI key
|
||||
if [ -z "$OPENAI_API_KEY" ]; then
|
||||
echo "Error: OPENAI_API_KEY environment variable is not set for the demo."
|
||||
exit 1
|
||||
fi
|
||||
|
||||
echo "Creating .env file..."
|
||||
echo "OPENAI_API_KEY=$OPENAI_API_KEY" > .env
|
||||
echo ".env file created with OPENAI_API_KEY."
|
||||
fi
|
||||
|
||||
# Step 3: Start Arch
|
||||
echo "Starting Arch with arch_config.yaml..."
|
||||
archgw up arch_config.yaml
|
||||
|
||||
# Step 4: Start developer services
|
||||
echo "Starting Network Agent using Docker Compose..."
|
||||
docker compose up -d # Run in detached mode
|
||||
}
|
||||
|
||||
# Function to stop the demo
|
||||
stop_demo() {
|
||||
# Step 1: Stop Docker Compose services
|
||||
echo "Stopping Network Agent using Docker Compose..."
|
||||
docker compose down
|
||||
|
||||
# Step 2: Stop Arch
|
||||
echo "Stopping Arch..."
|
||||
archgw down
|
||||
}
|
||||
|
||||
# Main script logic
|
||||
if [ "$1" == "down" ]; then
|
||||
stop_demo
|
||||
else
|
||||
# Default action is to bring the demo up
|
||||
start_demo
|
||||
fi
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
// File: src/main/java/com/example/weather/WeatherForecastApplication.java
|
||||
package weather;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class WeatherForecastApplication {
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(WeatherForecastApplication.class, args);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,54 @@
|
|||
package weather.controller;
|
||||
|
||||
import weather.model.DayForecast;
|
||||
import weather.model.WeatherForecastResponse;
|
||||
import weather.model.WeatherRequest;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.time.LocalDate;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
|
||||
@RestController
|
||||
public class WeatherController {
|
||||
|
||||
private Random random = new Random();
|
||||
|
||||
@PostMapping("/weather")
|
||||
public WeatherForecastResponse getRandomWeatherForecast(@RequestBody WeatherRequest req) {
|
||||
WeatherForecastResponse response = new WeatherForecastResponse();
|
||||
response.setLocation(req.getLocation());
|
||||
response.setUnits(req.getUnits());
|
||||
|
||||
List<DayForecast> forecasts = new ArrayList<>();
|
||||
for (int i = 0; i < req.getDays(); i++) {
|
||||
// Generate a random min temperature between 50 and 89 (inclusive)
|
||||
int minTemp = random.nextInt(90 - 50) + 50;
|
||||
// Generate a max temperature between (minTemp + 5) and (minTemp + 19)
|
||||
int maxTemp = random.nextInt(15) + (minTemp + 5);
|
||||
|
||||
double finalMinTemp = minTemp;
|
||||
double finalMaxTemp = maxTemp;
|
||||
|
||||
// Convert to Celsius if necessary
|
||||
if (req.getUnits().equalsIgnoreCase("celsius") || req.getUnits().equalsIgnoreCase("c")) {
|
||||
finalMinTemp = (minTemp - 32) * 5.0 / 9.0;
|
||||
finalMaxTemp = (maxTemp - 32) * 5.0 / 9.0;
|
||||
}
|
||||
|
||||
DayForecast dayForecast = new DayForecast();
|
||||
dayForecast.setDate(LocalDate.now().plusDays(i).toString());
|
||||
dayForecast.setMin(finalMinTemp);
|
||||
dayForecast.setMax(finalMaxTemp);
|
||||
dayForecast.setUnits(req.getUnits());
|
||||
|
||||
forecasts.add(dayForecast);
|
||||
}
|
||||
response.setDailyForecast(forecasts);
|
||||
return response;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,40 @@
|
|||
package weather.model;
|
||||
|
||||
public class DayForecast {
|
||||
private String date;
|
||||
private String units;
|
||||
private double min;
|
||||
private double max;
|
||||
|
||||
public DayForecast() {}
|
||||
|
||||
// Getters and setters
|
||||
public String getDate() {
|
||||
return date;
|
||||
}
|
||||
|
||||
public void setDate(String date) {
|
||||
this.date = date;
|
||||
}
|
||||
|
||||
public String getUnits() {
|
||||
return units;
|
||||
}
|
||||
|
||||
public void setUnits(String units) {
|
||||
this.units = units;
|
||||
}
|
||||
|
||||
public double getMin() {
|
||||
return min;
|
||||
}
|
||||
public void setMin(double min) {
|
||||
this.min = min;
|
||||
}
|
||||
public double getMax() {
|
||||
return max;
|
||||
}
|
||||
public void setMax(double max) {
|
||||
this.max = max;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,37 @@
|
|||
package weather.model;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class WeatherForecastResponse {
|
||||
private String location;
|
||||
private String units;
|
||||
private List<DayForecast> forecast;
|
||||
|
||||
// Default Constructor
|
||||
public WeatherForecastResponse() {}
|
||||
|
||||
// Getters and Setters
|
||||
public String getLocation() {
|
||||
return location;
|
||||
}
|
||||
|
||||
public void setLocation(String location) {
|
||||
this.location = location;
|
||||
}
|
||||
|
||||
public String getUnits() {
|
||||
return units;
|
||||
}
|
||||
|
||||
public void setUnits(String units) {
|
||||
this.units = units;
|
||||
}
|
||||
|
||||
public List<DayForecast> getDailyForecast() {
|
||||
return forecast;
|
||||
}
|
||||
|
||||
public void setDailyForecast(List<DayForecast> forecast) {
|
||||
this.forecast = forecast;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,29 @@
|
|||
package weather.model;
|
||||
|
||||
public class WeatherRequest {
|
||||
private String location;
|
||||
private int days = 7;
|
||||
private String units = "Farenheit";
|
||||
|
||||
public WeatherRequest() {}
|
||||
|
||||
// Getters and setters
|
||||
public String getLocation() {
|
||||
return location;
|
||||
}
|
||||
public void setLocation(String location) {
|
||||
this.location = location;
|
||||
}
|
||||
public int getDays() {
|
||||
return days;
|
||||
}
|
||||
public void setDays(int days) {
|
||||
this.days = days;
|
||||
}
|
||||
public String getUnits() {
|
||||
return units;
|
||||
}
|
||||
public void setUnits(String units) {
|
||||
this.units = units;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1 @@
|
|||
server.port=8081
|
||||
Loading…
Add table
Add a link
Reference in a new issue