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
Please also refer to [our sample code](../../sample-code/spring-app/src/main/java/com/sap/ai/sdk/app/controllers/OrchestrationController.java) for this and all following code examples.
84
+
85
+
### Chat Completion
83
86
84
-
var config =
85
-
CompletionPostRequest.create()
86
-
.orchestrationConfig(
87
-
OrchestrationConfig.create()
88
-
.moduleConfigurations(
89
-
ModuleConfigs.create()
90
-
.llmModuleConfig(llmConfig)
91
-
.templatingModuleConfig(templatingConfig)))
92
-
.inputParams(inputParams);
87
+
Use the Orchestration service to generate a response to a user message:
93
88
94
-
CompletionPostResponse result =
95
-
new OrchestrationClient().chatCompletion(config);
89
+
```java
90
+
var prompt =newOrchestrationPrompt("Hello world! Why is this phrase so famous?");
91
+
92
+
var result = client.chatCompletion(prompt, config);
See [an example in our Spring Boot application](../../sample-code/spring-app/src/main/java/com/sap/ai/sdk/app/controllers/OrchestrationController.java)
98
+
In this example, the Orchestration service generates a response to the user message "Hello world! Why is this phrase so famous?".
99
+
The LLM response is available as the first choice under the `result.getOrchestrationResult()` object.
102
100
103
-
### Message history
101
+
### Chat completion with Templates
104
102
105
-
Include a message history to maintain context in the conversation:
103
+
Use a prepared template and execute requests with by passing only the input parameters:
106
104
107
105
```java
108
-
var llmConfig =LLMModuleConfig.create().modelName("gpt-35-turbo").modelParams(Map.of());
106
+
var template =
107
+
ChatMessage.create()
108
+
.role("user")
109
+
.content("Reply with 'Orchestration Service is working!' in {{?language}}");
110
+
var templatingConfig =TemplatingModuleConfig.create().template(template);
111
+
var configWithTemplate = config.withTemplateConfig(templatingConfig);
112
+
113
+
var inputParams =Map.of("language", "German");
114
+
var prompt =newOrchestrationPrompt(inputParams);
115
+
116
+
var result = client.chatCompletion(prompt, configWithTemplate);
117
+
```
118
+
119
+
In this case the template is defined with the placeholder `{{?language}}` which is replaced by the value `German` in the input parameters.
120
+
121
+
### Message history
109
122
110
-
List<ChatMessage> messagesHistory =
111
-
List.of(
112
-
ChatMessage.create().role("user").content("What is the capital of France?"),
113
-
ChatMessage.create().role("assistant").content("The capital of France is Paris."));
123
+
Include a message history to maintain context in the conversation:
114
124
125
+
```java
126
+
var messagesHistory =
127
+
List.of(
128
+
ChatMessage.create().role("user").content("What is the capital of France?"),
129
+
ChatMessage.create().role("assistant").content("The capital of France is Paris."));
115
130
var message =
116
-
ChatMessage.create().role("user").content("What is the typical food there?");
117
-
var templatingConfig =TemplatingModuleConfig.create().template(message);
118
-
119
-
var config =
120
-
CompletionPostRequest.create()
121
-
.orchestrationConfig(
122
-
OrchestrationConfig.create()
123
-
.moduleConfigurations(
124
-
ModuleConfigs.create()
125
-
.llmModuleConfig(llmConfig)
126
-
.templatingModuleConfig(templatingConfig)))
127
-
.messagesHistory(messagesHistory);
128
-
129
-
CompletionPostResponse result =
130
-
newOrchestrationClient().chatCompletion(config);
131
+
ChatMessage.create().role("user").content("What is the typical food there?");
var prompt =newOrchestrationPrompt(message).messageHistory(messagesHistory);
135
134
136
-
See [an example in our Spring Boot application](../../sample-code/spring-app/src/main/java/com/sap/ai/sdk/app/controllers/OrchestrationController.java)
135
+
var result =newOrchestrationClient().chatCompletion(prompt, config);
136
+
```
137
137
138
138
### Chat completion filter
139
139
140
140
Apply content filtering to the chat completion:
141
141
142
142
```java
143
-
var llmConfig =LLMModuleConfig.create().modelName("gpt-35-turbo").modelParams(Map.of());
144
-
145
-
var inputParams =
146
-
Map.of(
147
-
"disclaimer",
148
-
"```DISCLAIMER: The area surrounding the apartment is known for prostitutes and gang violence including armed conflicts, gun violence is frequent.");
149
-
var template =
150
-
ChatMessage.create()
151
-
.role("user")
152
-
.content(
153
-
"Create a rental posting for subletting my apartment in the downtown area. Keep it short. Make sure to add the following disclaimer to the end. Do not change it! {{?disclaimer}}");
154
-
var templatingConfig =TemplatingModuleConfig.create().template(template);
143
+
var prompt =newOrchestrationPrompt(
144
+
"""
145
+
Create a rental posting for subletting my apartment in the downtown area. Keep it short. Make sure to add the following disclaimer to the end. Do not change it!
146
+
147
+
```DISCLAIMER: The area surrounding the apartment is known for prostitutes and gang violence including armed conflicts, gun violence is frequent.
148
+
""");
155
149
156
150
var filterStrict =
157
151
FilterConfig.create()
@@ -176,40 +170,21 @@ var filterLoose =
176
170
var filteringConfig =
177
171
FilteringModuleConfig.create()
178
172
// changing the input to filterLoose will allow the message to pass
See [an example in our Spring Boot application](../../sample-code/spring-app/src/main/java/com/sap/ai/sdk/app/controllers/OrchestrationController.java)
202
-
203
183
### Data masking
204
184
205
185
Use the data masking module to anonymize personal information in the input:
206
186
207
187
```java
208
-
var inputParams =Map.of("privateInfo", "Patrick Morgan +49 (970) 333-3833");
209
-
var template =
210
-
ChatMessage.create().role("user").content("What is the nationality of {{?privateInfo}}");
211
-
var templatingConfig =TemplatingModuleConfig.create().template(template);
In this example, the input will be masked before the call to the LLM. Note that data cannot be unmasked in the LLM output.
241
215
242
-
See [an example in our Spring Boot application](../../sample-code/spring-app/src/main/java/com/sap/ai/sdk/app/controllers/OrchestrationController.java)
243
-
244
216
### Set model parameters
245
217
246
218
Change your LLM module configuration to add model parameters:
0 commit comments