Docu: added more info on ChatLanguageModel API
This commit is contained in:
parent
43301bed5a
commit
34865af810
|
@ -16,7 +16,67 @@ Examples of such chat models include OpenAI's `gpt-3.5-turbo` and Google's `gemi
|
|||
Support for `LanguageModel`s will no longer be expanded in LangChain4j,
|
||||
so in all new features, we will use a `ChatLanguageModel` API.
|
||||
|
||||
Now, let's take a closer look at the ChatLanguageModel API.
|
||||
Now, let's take a closer look at the `ChatLanguageModel` API.
|
||||
|
||||
```java
|
||||
public interface ChatLanguageModel {
|
||||
|
||||
String generate(String userMessage);
|
||||
|
||||
...
|
||||
}
|
||||
```
|
||||
As you can see, there is a `generate` method that takes a `String` as input and returns a `String` as output, similar to `LanguageModel`.
|
||||
This is a convenience method so you can play around quickly and easily without needing to wrap the `String` in a `UserMessage`.
|
||||
|
||||
Let's continue:
|
||||
```java
|
||||
...
|
||||
|
||||
Response<AiMessage> generate(ChatMessage... messages);
|
||||
|
||||
Response<AiMessage> generate(List<ChatMessage> messages);
|
||||
|
||||
...
|
||||
```
|
||||
|
||||
These versions of the `generate` methods take one or multiple `ChatMessage`s as input.
|
||||
`ChatMessage` is a base interface that represents a chat message.
|
||||
|
||||
There are currently four implementations, one for each "source" of the message:
|
||||
|
||||
- `UserMessage`: This is a message from the user.
|
||||
The user can be either an end user of your application (a human) or your application itself.
|
||||
Depending on the modalities supported by the LLM, `UserMessage` can contain either just text (`String)`, or text and/or images (`Image`).
|
||||
- `AiMessage`: This is a message that was generated by the AI.
|
||||
As you might have noted, the generate method returns an `AiMessage` wrapped in a `Response`.
|
||||
`AiMessage` can contain either a textual response (`String`), or a request to execute a tool (`ToolExecutionRequest`).
|
||||
Don't worry, we will explore tools a bit later.
|
||||
- `ToolExecutionResultMessage`: This is the result of the `ToolExecutionRequest`. We will cover this more a bit later.
|
||||
- `SystemMessage`: This is a message from the system.
|
||||
Usually, you, as a developer, should define the content of this message.
|
||||
Normally, you would write here instructions on what the LLM's role is in this conversation,
|
||||
how it should behave, in what style to answer, etc.
|
||||
LLMs are trained to pay more attention to `SystemMessage` than to other types of messages,
|
||||
so be careful and it's better not to give an end user free access to define or inject some input into a `SystemMessage`.
|
||||
Usually, it is located at the start of the conversation.
|
||||
|
||||
Now that we know all types of `ChatMessage`, let's see how we can combine them in the conversation.
|
||||
|
||||
The easiest option is to provide a single instance of `UserMessage` into the `generate` method.
|
||||
This is similar to the first version of the `generate` method, which takes a `String` as input.
|
||||
The major difference here is that it now returns not a `String`, but a `Response<AiMessage>`.
|
||||
`Response` is a wrapper around content (payload), and you will often see it as a return type of `*Model` classes.
|
||||
In addition to content (in this case, `AiMessage`), `Response` also contains meta information about the generation.
|
||||
First, it includes `TokenUsage`, which contains stats about how many tokens the input
|
||||
(all the `ChatMessages` that you provided to the generate method) contained,
|
||||
how many tokens were generated as output (in the `AiMessage`), and the total (input + output).
|
||||
You will need this information to calculate how much a given call to the LLM costs.
|
||||
Then, `Response` also contains `FinishReason`, which is an enum with various reasons why generation has stopped.
|
||||
Usually, it will be `FinishReason.STOP`, if the LLM decided to stop generation itself.
|
||||
|
||||
There are multiple ways to create a `UserMessage`, depending on the contents.
|
||||
The easiest one is `new UserMessage("Hi")` or `UserMessage.from("Hi")`.
|
||||
|
||||
TODO
|
||||
|
||||
|
|
Loading…
Reference in New Issue