Skip to content

Commit

Permalink
add observation
Browse files Browse the repository at this point in the history
  • Loading branch information
alexcheng1982 committed Oct 5, 2024
1 parent 70ff89b commit 7f238ce
Show file tree
Hide file tree
Showing 12 changed files with 218 additions and 16 deletions.
15 changes: 15 additions & 0 deletions bootstrap/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,21 @@
<artifactId>launcher-ktor</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>io.github.llmagentbuilder</groupId>
<artifactId>open-telemetry</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>io.github.llmagentbuilder</groupId>
<artifactId>llm-openai</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>io.github.llmagentbuilder</groupId>
<artifactId>agent-planner-react-json</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import io.github.llmagentbuilder.core.*
import io.github.llmagentbuilder.core.tool.AgentToolFunctionCallbackContext
import io.github.llmagentbuilder.core.tool.AgentToolsProviderFactory
import io.github.llmagentbuilder.launcher.ktor.server.KtorLauncher
import io.github.llmagentbuilder.plugin.observation.opentelemetry.OpenTelemetryPlugin
import io.micrometer.observation.ObservationRegistry
import org.slf4j.LoggerFactory
import org.springframework.ai.chat.client.ChatClient
import org.springframework.ai.chat.client.advisor.MessageChatMemoryAdvisor
Expand Down Expand Up @@ -47,6 +49,8 @@ object AgentBootstrap {
AgentToolFunctionCallbackContext(
agentToolsProvider,
)
val observationRegistry = ObservationRegistry.create()
OpenTelemetryPlugin().install(agentConfig, observationRegistry)
val llmConfigs = agentConfig.llm
val chatModel = ServiceLoader.load(ChatModelProvider::class.java)
.stream()
Expand All @@ -60,8 +64,9 @@ object AgentBootstrap {
.firstOrNull()?.also {
logger.info("Loaded ChatModel $it")
} ?: throw RuntimeException("No ChatModel found")
val chatClientBuilder = ChatClient.builder(chatModel)
.defaultAdvisors(advisors)
val chatClientBuilder =
ChatClient.builder(chatModel, observationRegistry, null)
.defaultAdvisors(advisors)
val plannerConfigs = agentConfig.planner
val planner = ServiceLoader.load(PlannerProvider::class.java)
.stream()
Expand All @@ -82,9 +87,10 @@ object AgentBootstrap {
metadata?.description,
metadata?.usageInstruction,
agentToolsProvider,
UUID.randomUUID().toString(),
observationRegistry,
)
KtorLauncher.launch(chatAgent)
// JdkHttpSyncLauncher().launch(chatAgent, agentToolsProvider)
}

private fun profileAdvisor(agentConfig: AgentConfig): Advisor? {
Expand Down
8 changes: 7 additions & 1 deletion bootstrap/src/main/resources/agent.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
metadata:
name: Test agent
name: TestAgent
llm:
openai:
enabled: true
Expand All @@ -11,6 +11,12 @@ memory:
planner:
reActJson:
enabled: true
observation:
tracing:
exporter:
endpoint: "https://api.honeycomb.io/v1/traces"
headers:
"x-honeycomb-team": "{{env.HONEYCOMB_API_KEY}}"
tools:
- id: writeLocalFile
config:
Expand Down
1 change: 0 additions & 1 deletion cli/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
<dependency>
<groupId>com.github.jknack</groupId>
<artifactId>handlebars</artifactId>
<version>4.4.0</version>
</dependency>
<dependency>
<groupId>org.yaml</groupId>
Expand Down
4 changes: 4 additions & 0 deletions core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
</dependency>
<dependency>
<groupId>com.github.jknack</groupId>
<artifactId>handlebars</artifactId>
</dependency>

<dependency>
<groupId>uk.org.webcompere</groupId>
Expand Down
29 changes: 29 additions & 0 deletions core/src/main/kotlin/io/github/llmagentbuilder/core/AgentConfig.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.github.llmagentbuilder.core

import com.github.jknack.handlebars.Handlebars
import org.yaml.snakeyaml.LoaderOptions
import org.yaml.snakeyaml.Yaml
import org.yaml.snakeyaml.constructor.Constructor
Expand Down Expand Up @@ -36,13 +37,29 @@ class AgentMetadata {
var usageInstruction: String? = null
}

class TracingExporterConfig {
lateinit var endpoint: String
var headers: Map<String, String>? = null
}

class TracingConfig {
var enabled: Boolean? = false
var exporter: TracingExporterConfig? = null
}

class ObservationConfig {
var enabled: Boolean? = false
var tracing: TracingConfig? = null
}

class AgentConfig {
var metadata: AgentMetadata? = null
var profile: ProfileConfig? = null
var memory: MemoryConfig? = null
var llm: Map<String, Any?>? = null
var planner: Map<String, Any?>? = null
var tools: List<ToolConfig>? = null
var observation: ObservationConfig? = null
}

object AgentConfigLoader {
Expand All @@ -62,4 +79,16 @@ object AgentConfigLoader {
)
).load(reader)
}
}

object EvaluationHelper {
private val handlebars = Handlebars()

fun evaluate(input: String): String {
return handlebars.compileInline(input).apply(
mapOf(
"env" to System.getenv(),
)
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package io.github.llmagentbuilder.core

import io.micrometer.observation.ObservationRegistry

interface ObservationPlugin {
fun install(
agentConfig: AgentConfig,
observationRegistry: ObservationRegistry
)
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,3 @@
/**
* Agent Protocol
* Specification of the API protocol for communication with an agent.
*
* The version of the OpenAPI document: v1
*
*
* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
* https://openapi-generator.tech
* Do not edit the class manually.
*/
package io.github.llmagentbuilder.launcher.ktor.server.apis

import io.github.llmagentbuilder.core.ChatAgent
Expand All @@ -28,6 +17,11 @@ import kotlin.collections.set
fun Route.AgentApi(chatAgent: ChatAgent) {
val tasks = ConcurrentHashMap<String, String>()

post("/chat") {
val request = call.receive(ChatAgentRequest::class)
call.respond(chatAgent.call(request))
}

post("/ap/v1/agent/tasks") {
val taskId = UUID.randomUUID().toString()
val request = call.receive(TaskRequestBody::class)
Expand Down
47 changes: 47 additions & 0 deletions observation/open-telemetry/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?xml version="1.0" encoding="UTF-8"?>
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>io.github.llmagentbuilder</groupId>
<artifactId>observation</artifactId>
<version>0.2.1</version>
</parent>

<artifactId>open-telemetry</artifactId>

<properties>
<maven.compiler.source>21</maven.compiler.source>
<maven.compiler.target>21</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<opentelemetry.version>1.42.1</opentelemetry.version>
</properties>

<dependencies>
<dependency>
<groupId>io.github.llmagentbuilder</groupId>
<artifactId>core</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>io.opentelemetry</groupId>
<artifactId>opentelemetry-sdk</artifactId>
<version>${opentelemetry.version}</version>
</dependency>
<dependency>
<groupId>io.opentelemetry</groupId>
<artifactId>opentelemetry-exporter-otlp</artifactId>
<version>${opentelemetry.version}</version>
</dependency>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-otlp</artifactId>
</dependency>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-tracing-bridge-otel</artifactId>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package io.github.llmagentbuilder.plugin.observation.opentelemetry

import io.github.llmagentbuilder.core.AgentConfig
import io.github.llmagentbuilder.core.EvaluationHelper
import io.github.llmagentbuilder.core.ObservationPlugin
import io.micrometer.observation.ObservationRegistry
import io.micrometer.tracing.handler.DefaultTracingObservationHandler
import io.micrometer.tracing.otel.bridge.EventListener
import io.micrometer.tracing.otel.bridge.OtelCurrentTraceContext
import io.micrometer.tracing.otel.bridge.OtelTracer
import io.micrometer.tracing.otel.bridge.OtelTracer.EventPublisher
import io.micrometer.tracing.otel.bridge.Slf4JEventListener
import io.opentelemetry.exporter.otlp.http.trace.OtlpHttpSpanExporter
import io.opentelemetry.sdk.resources.Resource
import io.opentelemetry.sdk.trace.SdkTracerProvider
import io.opentelemetry.sdk.trace.export.BatchSpanProcessor

class OpenTelemetryPlugin : ObservationPlugin {
override fun install(
agentConfig: AgentConfig,
observationRegistry: ObservationRegistry
) {
val builder = Resource.builder()
builder.put("service.name", agentConfig.metadata?.name)
val exporterBuilder = OtlpHttpSpanExporter.builder()
agentConfig.observation?.tracing?.exporter?.let {
exporterBuilder.setEndpoint(it.endpoint)
it.headers?.entries?.forEach { (k, v) ->
exporterBuilder.addHeader(k, EvaluationHelper.evaluate(v))
}
}
val resource = builder.build()
val tracer = SdkTracerProvider.builder()
.setResource(resource)
.addSpanProcessor(
BatchSpanProcessor.builder(
exporterBuilder.build()
).build()
)
.build()
.get("llm-agent")
val context = OtelCurrentTraceContext()
val otelTracer = OtelTracer(
tracer, context, OTelEventPublisher(
listOf(
Slf4JEventListener()
)
)
)
val handler = DefaultTracingObservationHandler(otelTracer)
observationRegistry.observationConfig().observationHandler(handler)
}

private class OTelEventPublisher(private val listeners: List<EventListener>) :
EventPublisher {
override fun publishEvent(event: Any?) {
listeners.forEach { it.onEvent(event) }
}
}
}
25 changes: 25 additions & 0 deletions observation/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="UTF-8"?>
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>io.github.llmagentbuilder</groupId>
<artifactId>llm-agent-builder</artifactId>
<version>0.2.1</version>
</parent>

<artifactId>observation</artifactId>
<packaging>pom</packaging>
<name>LLM Observation</name>
<modules>
<module>open-telemetry</module>
</modules>

<properties>
<maven.compiler.source>21</maven.compiler.source>
<maven.compiler.target>21</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

</project>
7 changes: 7 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
<module>bootstrap</module>
<module>llm</module>
<module>cli</module>
<module>observation</module>
</modules>

<description>LLM Agent Builder</description>
Expand Down Expand Up @@ -57,6 +58,7 @@
<jackson.version>2.16.1</jackson.version>
<micrometer.version>1.12.4</micrometer.version>
<micrometer-tracing.version>1.2.4</micrometer-tracing.version>
<handlebars.version>4.4.0</handlebars.version>
</properties>

<build>
Expand Down Expand Up @@ -290,6 +292,11 @@
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>com.github.jknack</groupId>
<artifactId>handlebars</artifactId>
<version>${handlebars.version}</version>
</dependency>
</dependencies>
</dependencyManagement>

Expand Down

0 comments on commit 7f238ce

Please sign in to comment.