Skip to content

Commit 3c05cde

Browse files
authored
refactor: change granite-io block syntax (#975)
Signed-off-by: Louis Mandel <[email protected]>
1 parent dba3cab commit 3c05cde

27 files changed

+462
-176
lines changed

examples/demos/granite_io_hallucinations.pdl

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
description: GraniteIO halluncination example
1+
description: GraniteIO hallucination example
22
defs:
33
doc:
44
data:
@@ -40,13 +40,16 @@ defs:
4040

4141
text:
4242
- Did Faith Hill take a break from recording after releasing her second album, It Matters to Me?
43-
- model: "granite3.2:2b"
44-
backend: openai
43+
- processor:
44+
model: "granite3.2:2b"
45+
backend: openai
4546
parameters:
4647
documents:
4748
- ${ doc }
4849
controls:
4950
hallucinations: true
51+
generate_inputs:
52+
temperature: 0.0
5053
modelResponse: output
5154
- "\nHallucinations:\n"
5255
- for:

examples/granite-io/granite_io_hallucinations.pdl

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
description: GraniteIO hallucination example
12
defs:
23
doc:
34
data:
@@ -39,14 +40,16 @@ defs:
3940

4041
text:
4142
- Did Faith Hill take a break from recording after releasing her second album, It Matters to Me?
42-
- model: "granite3.2:2b"
43-
backend: openai
43+
- processor:
44+
model: "granite3.2:2b"
45+
backend: openai
4446
parameters:
4547
documents:
4648
- ${ doc }
4749
controls:
4850
hallucinations: true
49-
citations: true
51+
generate_inputs:
52+
temperature: 0.0
5053
modelResponse: output
5154
- "\nHallucinations:\n"
5255
- for:
@@ -57,12 +60,3 @@ text:
5760
- "\nSentence: ${ hallucination.response_text }"
5861
join:
5962
with: "\n"
60-
- "\n\nCitations:\n"
61-
- for:
62-
citation: ${ output.results[0].next_message.citations }
63-
repeat:
64-
text:
65-
- "Citation: ${ citation.context_text }"
66-
- "\nSentence: ${ citation.response_text }"
67-
join:
68-
with: "\n"
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
defs:
2+
io_proc:
3+
lang: python
4+
code: |
5+
from granite_io import make_backend, make_io_processor
6+
model_name = "granite3.2:2b"
7+
backend = make_backend("openai", { "model_name": model_name })
8+
result = make_io_processor(model_name, backend=backend)
9+
text:
10+
- "Hello!\n"
11+
- processor: ${ io_proc }
12+
parameters:
13+
generate_inputs:
14+
temperature: 0.0
Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
text:
22
- "Hello!\n"
3-
- model: "granite3.2:2b"
4-
backend: openai
3+
- processor:
4+
model: "granite3.2:2b"
5+
backend: openai
6+
parameters:
7+
generate_inputs:
8+
temperature: 0.0
Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
text:
2-
- |
3-
Find the fastest way for a seller to visit all the cities in their region
4-
>> Response:
5-
- model: "granite3.2:2b"
6-
backend: openai
2+
- Find the fastest way for a seller to visit all the cities in their region
3+
- def: response
4+
processor:
5+
model: "granite3.2:2b"
6+
backend: openai
77
parameters:
88
thinking: true
9+
generate_inputs:
10+
temperature: 0.0
911
modelResponse: outputs
10-
- "\n"
11-
- |
12-
>> Thoughts:
13-
${ outputs.results[0].next_message.reasoning_content }
12+
contribute: []
13+
- |
14+
15+
>> Thoughts:
16+
${ outputs.results[0].next_message.reasoning_content }
17+
>> Response:
18+
${ response }
Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
text:
22
- "Hello!\n"
3-
- model: ibm-granite/granite-3.2-2b-instruct
4-
backend:
5-
transformers: cpu
6-
processor: granite3.2
3+
- processor:
4+
type: granite3.2
5+
model: ibm-granite/granite-3.2-2b-instruct
6+
backend:
7+
transformers: cpu

pdl-live-react/src/helpers.ts

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import type {
99
PdlModelInput,
1010
LocalizedExpression,
1111
} from "./pdl_ast"
12+
import { match, P } from "ts-pattern"
1213

1314
/** Re-export for convenience */
1415
export type { PdlBlock } from "./pdl_ast"
@@ -325,10 +326,27 @@ function isExpr(e: unknown): e is LocalizedExpression {
325326
)
326327
}
327328

328-
export function extractModel({ model }: ModelBlock): string {
329-
return typeof model === "string"
330-
? model
331-
: isExpr(model)
332-
? String(model.pdl__result)
333-
: "unknown"
329+
function getValue<T>(expr: ExpressionT<T>) {
330+
if (isExpr(expr)) {
331+
if (expr.pdl__result === undefined) {
332+
return expr.pdl__expr
333+
} else {
334+
return expr.pdl__result
335+
}
336+
} else {
337+
return expr
338+
}
339+
}
340+
341+
export function extractModel(block: ModelBlock): string {
342+
return match(block)
343+
.with({ model: P._ }, (block) => stringify(getValue(block.model)))
344+
.with({ processor: { model: P._ } }, (block) =>
345+
stringify(getValue(block.processor.model)),
346+
)
347+
.with({ processor: { type: P._ } }, (block) =>
348+
stringify(getValue(block.processor.type)),
349+
)
350+
.with({ processor: P._ }, (block) => stringify(getValue(block.processor)))
351+
.exhaustive()
334352
}

pdl-live-react/src/pdl_ast.d.ts

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2354,11 +2354,6 @@ export type PdlContext17 =
23542354
export type PdlId17 = string | null
23552355
export type PdlIsLeaf17 = true
23562356
export type Kind17 = "model"
2357-
/**
2358-
* Model name used by the backend.
2359-
*
2360-
*/
2361-
export type Model = LocalizedExpression | string
23622357
/**
23632358
* Messages to send to the model.
23642359
*
@@ -2502,11 +2497,6 @@ export type PdlContext18 =
25022497
export type PdlId18 = string | null
25032498
export type PdlIsLeaf18 = true
25042499
export type Kind18 = "model"
2505-
/**
2506-
* Name of the model following the LiteLLM convention.
2507-
*
2508-
*/
2509-
export type Model1 = LocalizedExpression | string
25102500
/**
25112501
* Messages to send to the model.
25122502
*
@@ -2552,6 +2542,11 @@ export type PdlModelInput1 =
25522542
*
25532543
*/
25542544
export type Platform1 = "litellm"
2545+
/**
2546+
* Name of the model following the LiteLLM convention.
2547+
*
2548+
*/
2549+
export type Model1 = LocalizedExpression | string
25552550
/**
25562551
* Parameters to send to the model.
25572552
*
@@ -3113,7 +3108,6 @@ export interface LitellmModelBlock {
31133108
pdl__timing?: PdlTiming | null
31143109
pdl__is_leaf?: PdlIsLeaf18
31153110
kind?: Kind18
3116-
model: Model1
31173111
input?: Input1
31183112
modelResponse?: Modelresponse1
31193113
/**
@@ -3123,6 +3117,7 @@ export interface LitellmModelBlock {
31233117
pdl__usage?: PdlUsage | null
31243118
pdl__model_input?: PdlModelInput1
31253119
platform?: Platform1
3120+
model: Model1
31263121
parameters?: Parameters1
31273122
}
31283123
/**
@@ -3200,7 +3195,6 @@ export interface GraniteioModelBlock {
32003195
pdl__timing?: PdlTiming | null
32013196
pdl__is_leaf?: PdlIsLeaf17
32023197
kind?: Kind17
3203-
model: Model
32043198
input?: Input
32053199
modelResponse?: Modelresponse
32063200
/**
@@ -3210,8 +3204,7 @@ export interface GraniteioModelBlock {
32103204
pdl__usage?: PdlUsage | null
32113205
pdl__model_input?: PdlModelInput
32123206
platform?: Platform
3213-
backend: unknown
3214-
processor?: unknown
3207+
processor: unknown
32153208
parameters?: Parameters
32163209
}
32173210
/**

pdl-live-react/src/pdl_ast_utils.ts

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -62,29 +62,42 @@ export function map_block_children(
6262
{
6363
kind: "model",
6464
platform: "granite-io",
65-
backend: P.nonNullable,
6665
processor: P._,
6766
},
6867
(block) => {
69-
const model = f_expr(block.model)
68+
const processor = match(block.processor)
69+
.with(
70+
{
71+
type: P.union(P._, P.nullish),
72+
model: P.union(P._, P.nullish),
73+
backend: P.nonNullable,
74+
},
75+
(proc) => {
76+
const type = proc.type ? f_expr(proc.type) : undefined
77+
const model = proc.model ? f_expr(proc.model) : undefined
78+
const backend = f_expr(proc.backend)
79+
return {
80+
...proc,
81+
type,
82+
model,
83+
backend,
84+
}
85+
},
86+
)
87+
.otherwise((proc) => f_expr(proc))
7088
const input = block.input ? f_block(block.input) : undefined
71-
// @ts-expect-error: f_expr does not preserve the type of the expression
72-
const parameters: Parameters = block.parameters
89+
const parameters = block.parameters
7390
? f_expr(block.parameters)
7491
: undefined
75-
const backend = f_expr(block.backend)
76-
const processor = block.processor ? f_expr(block.processor) : undefined
7792
return {
7893
...block,
79-
model,
94+
processor,
8095
input,
8196
parameters,
82-
backend,
83-
processor,
8497
}
8598
},
8699
)
87-
.with({ kind: "model" }, (block) => {
100+
.with({ kind: "model", model: P._ }, (block) => {
88101
const model = f_expr(block.model)
89102
const input = block.input ? f_block(block.input) : undefined
90103
const parameters = block.parameters ? f_expr(block.parameters) : undefined
@@ -96,6 +109,15 @@ export function map_block_children(
96109
parameters,
97110
}
98111
})
112+
.with({ kind: "model" }, (block) => {
113+
const input = block.input ? f_block(block.input) : undefined
114+
const parameters = block.parameters ? f_expr(block.parameters) : undefined
115+
return {
116+
...block,
117+
input,
118+
parameters,
119+
}
120+
})
99121
.with({ kind: "code" }, (block) => {
100122
if (isArgs(block)) {
101123
const args = block.args.map((arg) => f_expr(arg))

0 commit comments

Comments
 (0)