-
Notifications
You must be signed in to change notification settings - Fork 2
/
Program.cs
163 lines (119 loc) · 5.55 KB
/
Program.cs
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
using LangChainJSDotNet;
using Microsoft.Extensions.Configuration;
namespace LangChainJSDemo
{
public class HostObject
{
public async Task<string> FooAsync(string input)
{
// Simulate an async operation
await Task.Delay(100);
return "5";
}
}
internal class Program
{
private static IConfiguration Configuration { get; } = new ConfigurationBuilder()
.AddJsonFile("appsettings.json", optional: false)
.AddUserSecrets<Program>()
.Build();
static async Task Main(string[] args)
{
//await RunModelAndPrint();
//await RunModelAndReturn();
//await RunChain();
await RunAgent();
}
static async Task RunModelAndPrint()
{
using var langchainjs = new LangChainJS();
langchainjs.SetEnvironmentVariable("OPENAI_API_KEY", Configuration["OPENAI_API_KEY"]);
langchainjs.Setup(@"
const model = new OpenAI();
globalThis.run = async () => {
const result = await model.call('What would be a good company name a company that makes colorful socks?');
console.log(result.trim());
}
");
await langchainjs.InvokeAsync("run");
}
static async Task RunModelAndReturn()
{
using var langchainjs = new LangChainJS();
langchainjs.SetEnvironmentVariable("OPENAI_API_KEY", Configuration["OPENAI_API_KEY"]);
langchainjs.Setup(@"
const model = new OpenAI({ temperature: 0.9 });
globalThis.run = async () => {
const res = await model.call('What would be a good company name a company that makes colorful socks?');
return res.trim();
}
");
var result = await langchainjs.InvokeAsync<string>("run");
Console.WriteLine(result);
}
static async Task RunChain()
{
using var langchainjs = new LangChainJS(enableDebugging: false);
langchainjs.SetEnvironmentVariable("OPENAI_API_KEY", Configuration["OPENAI_API_KEY"]);
langchainjs.Setup(@"
const model = new OpenAI({ temperature: 0.9 });
const template = new PromptTemplate({
template: 'What is a good name for a company that makes {product}?',
inputVariables: ['product'],
});
chain = new LLMChain({ llm: model, prompt: template });
globalThis.run = async (prompt) => {
const res = await chain.call({ product: prompt });
return res.text.trim();
}
");
string result = await langchainjs.InvokeAsync<string>("run", "colorful socks");
Console.WriteLine(result);
}
static async Task RunAgent()
{
using var langchainjs = new LangChainJS(enableDebugging: false);
langchainjs.SetEnvironmentVariable("OPENAI_API_KEY", Configuration["OPENAI_API_KEY"]);
langchainjs.AddHostObject("host", new HostObject());
langchainjs.Setup(@"
const model = new ChatOpenAI({ temperature: 0 });
const tools = [
new DynamicTool({
name: ""FOO"",
description:
""call this to get the value of foo. input should be the name of the user."",
func: async (input) => await host.FooAsync(input),
}),
new DynamicTool({
name: ""BAR"",
description:
""call this to get the value of bar. input should be an empty string."",
func: async () => ""baz"",
}),
// Input must be an object with 'high' and 'low' numbers.
new DynamicStructuredTool({
name: ""number-substructor"",
description: ""substructs one number by the other."",
schema: z.object({
high: z.number().describe(""The upper number""),
low: z.number().describe(""The lower number""),
}),
func: async ({ high, low }) => (high - low).toString() // Outputs must be strings
}),
];
globalThis.run = async (input) => {
// zero-shot-react-description
const executor = await initializeAgentExecutorWithOptions(tools, model, {
agentType: ""structured-chat-zero-shot-react-description"",
/* verbose: true */
});
console.log(`Agent input: ""${input}""...`);
const result = await executor.call({ input });
return result.output;
};
");
string result = await langchainjs.InvokeAsync<string>("run", "What is the result if you substruct 8 by foo?");
Console.WriteLine(result);
}
}
}