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
Next, we need to install the Tanuki type transformer. This will allow Tanuki to be aware of your patched functions and types at runtime, as these types are usually erased by the Typescript compiler when transpiling into Javascript.
91
+
#### Default Setup
92
+
Next, we need to install the Tanuki type transformer. This will allow Tanuki to be aware of your patched functions and types at runtime, as these types are erased when transpiling into Javascript.
88
93
```typescript
89
94
npminstallts-patch--save-dev
90
95
npxts-patchinstall
@@ -103,15 +108,28 @@ Next, you need to add the Tanuki transformer to your `tsconfig.json` file:
103
108
}
104
109
}
105
110
```
106
-
This is required for Tanuki to be aware of your patched functions and types at runtime, as these types are usually erased by the Typescript compiler when transpiling into Javascript.
107
111
112
+
#### Next.js Setup
113
+
As Next.js has it's own build system, you must explicitly add the Tanuki transformer to your `package.json` file instead by adding the following scripts.
114
+
115
+
```json
116
+
{
117
+
"scripts": {
118
+
"predev": "tanuki-type-compiler",
119
+
"prebuild": "tanuki-type-compiler",
120
+
"prestart": "tanuki-type-compiler"
121
+
}
122
+
}
123
+
```
124
+
125
+
This will ensure that Tanuki can extract your patched functions before the Next.js build process.
108
126
109
127
110
128
<!-- TOC --><aname="getting-started"></a>
111
129
### Getting Started
112
130
113
131
To get started:
114
-
1. Create a `patch` function stub, including your input and output types, and an instruction.
132
+
1. Create a `patch` function stub as a static member of a class, including your input and output types, and an instruction.
115
133
2. (Optional) Create jest-like equivalent assertions in a `Tanuki.align` block, declaring the expected behaviour of your patched function with different inputs.
116
134
117
135
Once you have built your code (to make Tanuki aware of your types), the `patch` function will be registered and can be invoked as normal.
@@ -133,26 +151,25 @@ type Message = string;
133
151
* Declare the function that you want Tanuki to provide.
134
152
*/
135
153
classFunctions {
136
-
classifySentiment =patch<Sentiment, Message>()`Classifies message from the user based on sentiment`;
`Classifies message from the user based on sentiment`;
137
156
}
138
157
139
158
/**
140
159
* Align your function to the expected behavior using Jest-like assertions.
141
160
*/
142
161
Tanuki.align(async (it) => {
143
162
it("alignClassifySentiment", async (expect) => {
144
-
const functions =newFunctions();
145
-
expect(awaitfunctions.classifySentiment("I love you")).toEqual('Good');
146
-
expect(awaitfunctions.classifySentiment("I hate you")).toEqual('Bad');
147
-
expect(awaitfunctions.classifySentiment("People from Phoenix are called Phoenicians")).toBeNull();
163
+
expect(awaitFunctions.classifySentiment("I love you")).toEqual('Good');
164
+
expect(awaitFunctions.classifySentiment("I hate you")).toEqual('Bad');
165
+
expect(awaitFunctions.classifySentiment("People from Phoenix are called Phoenicians")).toBeNull();
148
166
});
149
167
});
150
168
151
169
// Example usage of the patched function somewhere else in your code
152
170
const runExamples =async () => {
153
-
const functions =newFunctions();
154
-
console.log(awaitfunctions.classifySentiment("I like you")); // Expect 'Good' or null
155
-
console.log(awaitfunctions.classifySentiment("Apples might be red")); // Expect null
171
+
console.log(awaitFunctions.classifySentiment("I like you")); // Expect 'Good' or null
172
+
console.log(awaitFunctions.classifySentiment("Apples might be red")); // Expect null
156
173
};
157
174
158
175
runExamples();
@@ -264,15 +281,16 @@ Test-Driven Alignment (TDA) adapts this concept to align the behavior of a patch
264
281
265
282
To align the behaviour of your patched function to your needs, decorate a function with `@align` and assert the outputs of the function with the ‘assert’ statement as is done with standard tests.
266
283
267
-
```python
268
-
@tanuki.align
269
-
defalign_classify_sentiment():
270
-
assert classify_sentiment("I love this!") =='Good'
271
-
assert classify_sentiment("I hate this.") =='Bad'
272
-
273
-
@tanuki.align
274
-
defalign_score_sentiment():
275
-
assert score_sentiment("I like you") ==7
284
+
```typescript
285
+
import { Finance } from'./finance';
286
+
import { Tanuki } from'tanuki.ts';
287
+
288
+
Tanuki.align((it) => {
289
+
it("should extract company names whose stock is increasing", async (expect) => {
290
+
const input1 ="Consumer spending makes up a huge fraction of the overall economy. Investors are therefore always looking at consumers to try to gauge whether their financial condition remains healthy. That's a big part of why the stock market saw a bear market in 2022, as some feared that a consumer-led recession would result in much weaker business performance across the sector.\nHowever, that much-anticipated recession hasn't happened yet, and there's still plenty of uncertainty about the future direction of consumer-facing stocks. A pair of earnings reports early Wednesday didn't do much to resolve the debate, as household products giant Procter & Gamble (PG 0.13%) saw its stock rise even as recreational vehicle manufacturer Winnebago Industries (WGO 0.58%) declined.";
By writing a test that encapsulates the expected behaviour of the tanuki-patched function, you declare the contract that the function must fulfill. This enables you to:
@@ -307,7 +325,7 @@ We tested out model distillation using Tanuki using OpenAI models on Squad2, Spi
Tanuki is a simple and seamless way to create LLM augmented functions in python, which ensure the outputs of the LLMs follow a specific structure. Moreover, the more you call a patched function, the cheaper and faster the execution gets.
328
+
Tanuki is a simple and seamless way to create LLM augmented functions in Typescript and Python, which ensure the outputs of the LLMs follow a specific structure. Moreover, the more you call a patched function, the cheaper and faster the execution gets.
0 commit comments