-
Notifications
You must be signed in to change notification settings - Fork 0
/
simple-openai-chat.service.ts
50 lines (38 loc) · 1.42 KB
/
simple-openai-chat.service.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import { StringOutputParser } from "@langchain/core/output_parsers";
import { HandlebarsPromptTemplate } from "langchain/experimental/prompts/handlebars";
import { ChatOpenAI } from "@langchain/openai";
import { Injectable } from "@nestjs/common";
@Injectable()
export class SimpleOpenAIChatService {
constructor(private readonly openAI: ChatOpenAI) {}
async chat(prompt: string): Promise<string> {
return (await this.openAI.invoke(prompt)).content as string;
}
private stringOutputParser = new StringOutputParser();
private promptTemplate = HandlebarsPromptTemplate.fromTemplate(
`After INPUT you will receive a possibly incorrectly formatted address.
It should include a street, houseNumber, city and a zipCode.
Return the address as json in the format:
{
"street": string,
"houseNumber": string,
"zipCode: string,
"city": string
}
Example:
{
"street": "Riesstraße",
"houseNumber": "22",
"zipCode: "80992",
"city": "München"
}
If any field is missing, set the value to null.
Return only the json no additional explanation, text or formatting.
INPUT
{{inputAddress}}`,
);
async chatWithTemplate(address: string): Promise<string> {
const llmChain = this.promptTemplate.pipe(this.openAI).pipe(this.stringOutputParser);
return llmChain.invoke({ inputAddress: address });
}
}