You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Each AI method creates its own span using the `langchain4j.aiservices.$interface_name.$method_name` template for the name.
382
-
Furthermore, tool invocations also create a span using `langchain4j.tools.$tool_name` template for the name.
383
-
384
-
385
-
For example, if the AI service looks like:
386
-
387
-
[source,java]
388
-
----
389
-
@RegisterAiService(tools = EmailService.class)
390
-
public interface PoemAiService {
391
-
392
-
@SystemMessage("You are a professional poet")
393
-
@UserMessage("Write a poem about {topic}. The poem should be {lines} lines long. Then send this poem by email.")
394
-
String writeAPoem(String topic, int lines);
395
-
396
-
}
397
-
----
398
-
399
-
a tool that looks like:
400
-
401
-
[source,java]
402
-
----
403
-
@ApplicationScoped
404
-
public class EmailService {
405
-
406
-
@Inject
407
-
Mailer mailer;
408
-
409
-
@Tool("send the given content by email")
410
-
public void sendAnEmail(String content) {
411
-
Log.info("Sending an email: " + content);
412
-
mailer.send(Mail.withText("[email protected]", "A poem for you", content));
413
-
}
414
-
415
-
}
416
-
----
417
-
418
-
and invocation of the AI service that looks like:
419
-
420
-
[source,java]
421
-
----
422
-
@Path("/email-me-a-poem")
423
-
public class EmailMeAPoemResource {
424
-
425
-
private final MyAiService service;
426
-
427
-
public EmailMeAPoemResource(MyAiService service) {
428
-
this.service = service;
429
-
}
430
-
431
-
@GET
432
-
public String emailMeAPoem() {
433
-
return service.writeAPoem("Quarkus", 4);
434
-
}
435
-
436
-
}
437
-
----
438
-
439
-
then an example trace is:
440
-
441
-
image::trace.png[width=1000,align="center"]
442
-
443
-
In the trace above we can see the parent span which corresponds to the handling the GET HTTP request, but the real
444
-
interesting thing is the `langchain4j.aiservices.MyAiService.writeAPoem` span which corresponds to the invocation of the AI service.
445
-
The child spans of this span correspond (from to right) to calling the OpenAI API, invoking the `sendEmail` tool and finally invoking calling the OpenAI API again.
446
-
447
-
==== Custom span data
448
-
if you have the need for custom span data, you can simply add a bean implemtenting `ChatModelSpanContributor`.
The extension allows users to audit the process of implementing an AiService by observing normal CDI events. The following example shows a class that audits all events.
487
-
488
-
NOTE: These methods do not all need to live in the same class and the name of the class and the methods do not matter. It is only shown this way for demonstration purposes.
489
-
490
-
[source,java]
491
-
----
492
-
@ApplicationScoped
493
-
public class AuditingListener {
494
-
public void initialMessagesCreated(@Observes InitialMessagesCreatedEvent initialMessagesCreatedEvent) {
495
-
// Invoked when the original user and system messages have been created
496
-
}
497
-
498
-
public void llmInteractionComplete(@Observes LLMInteractionCompleteEvent llmInteractionCompleteEvent) {
499
-
// Invoked when the final result of the AiService method has been computed
500
-
}
501
-
502
-
public void llmInteractionFailed(@Observes LLMInteractionFailureEvent llmInteractionFailureEvent) {
503
-
// Invoked when there was an exception computing the result of the AiService method
504
-
}
505
-
506
-
public void responseFromLLMReceived(@Observes ResponseFromLLMReceivedEvent responseFromLLMReceivedEvent) {
507
-
// Invoked with a response from an LLM.
508
-
// It is important to note that this can be invoked multiple times when tools exist.
509
-
}
510
-
511
-
public void toolExecuted(@Observes ToolExecutedEvent toolExecutedEvent) {
512
-
// Invoked with a tool response from an LLM.
513
-
// It is important to note that this can be invoked multiple times when tools exist.
514
-
}
515
-
}
516
-
----
517
-
518
337
== Working with images
519
338
520
339
An _AI Service_ can also be used when working with images, both to describe an image and to generate one.
@@ -551,3 +370,7 @@ Generating images with an _AI Service_ comes with restrictions compared to text
551
370
* Although auditing does work, it is however limited
552
371
* Guardrails only work on the input
553
372
====
373
+
374
+
== Observability
375
+
376
+
Refer to link:https://docs.quarkiverse.io/quarkus-langchain4j/dev/observability.html[this page] to learn how to trace LangChain4j applications.
0 commit comments