Skip to content

Commit d166bc3

Browse files
committed
granite-io hallucination demo example and notebook
Signed-off-by: Mandana Vaziri <[email protected]>
1 parent 9839214 commit d166bc3

File tree

2 files changed

+48
-136
lines changed

2 files changed

+48
-136
lines changed

examples/granite-io/granite_io_hallucinations.pdl

+19-2
Original file line numberDiff line numberDiff line change
@@ -47,5 +47,22 @@ text:
4747
controls:
4848
hallucinations: true
4949
citations: true
50-
modelResponse: outputs
51-
- ${ outputs }
50+
modelResponse: output
51+
- "\nHallucinations:\n"
52+
- for:
53+
hallucination: ${ output.results[0].next_message.hallucinations }
54+
repeat:
55+
text:
56+
- "Hallucination Risk: ${ hallucination.risk }"
57+
- "\nSentence: ${ hallucination.response_text }"
58+
join:
59+
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"

examples/notebooks/granite_io_demo.ipynb

+29-134
Original file line numberDiff line numberDiff line change
@@ -28,50 +28,10 @@
2828
},
2929
{
3030
"cell_type": "code",
31-
"execution_count": 1,
31+
"execution_count": null,
3232
"id": "e25a6874-54d9-4167-82ed-ab2f4fdc0a6f",
3333
"metadata": {},
34-
"outputs": [
35-
{
36-
"name": "stderr",
37-
"output_type": "stream",
38-
"text": [
39-
"ERROR:root:Error in adding the context spans to citation: Cited text not found in corresponding document\n",
40-
"ERROR:asyncio:Task exception was never retrieved\n",
41-
"future: <Task finished name='Task-14' coro=<AsyncClient.aclose() done, defined at /Users/lmandel/.pyenv/versions/3.13.0/envs/pdl-3.13/lib/python3.13/site-packages/httpx/_client.py:2024> exception=RuntimeError('Event loop is closed')>\n",
42-
"Traceback (most recent call last):\n",
43-
" File \"/Users/lmandel/.pyenv/versions/3.13.0/envs/pdl-3.13/lib/python3.13/site-packages/httpx/_client.py\", line 2031, in aclose\n",
44-
" await self._transport.aclose()\n",
45-
" File \"/Users/lmandel/.pyenv/versions/3.13.0/envs/pdl-3.13/lib/python3.13/site-packages/httpx/_transports/default.py\", line 389, in aclose\n",
46-
" await self._pool.aclose()\n",
47-
" File \"/Users/lmandel/.pyenv/versions/3.13.0/envs/pdl-3.13/lib/python3.13/site-packages/httpcore/_async/connection_pool.py\", line 353, in aclose\n",
48-
" await self._close_connections(closing_connections)\n",
49-
" File \"/Users/lmandel/.pyenv/versions/3.13.0/envs/pdl-3.13/lib/python3.13/site-packages/httpcore/_async/connection_pool.py\", line 345, in _close_connections\n",
50-
" await connection.aclose()\n",
51-
" File \"/Users/lmandel/.pyenv/versions/3.13.0/envs/pdl-3.13/lib/python3.13/site-packages/httpcore/_async/connection.py\", line 173, in aclose\n",
52-
" await self._connection.aclose()\n",
53-
" File \"/Users/lmandel/.pyenv/versions/3.13.0/envs/pdl-3.13/lib/python3.13/site-packages/httpcore/_async/http11.py\", line 258, in aclose\n",
54-
" await self._network_stream.aclose()\n",
55-
" File \"/Users/lmandel/.pyenv/versions/3.13.0/envs/pdl-3.13/lib/python3.13/site-packages/httpcore/_backends/anyio.py\", line 53, in aclose\n",
56-
" await self._stream.aclose()\n",
57-
" File \"/Users/lmandel/.pyenv/versions/3.13.0/envs/pdl-3.13/lib/python3.13/site-packages/anyio/_backends/_asyncio.py\", line 1306, in aclose\n",
58-
" self._transport.close()\n",
59-
" ~~~~~~~~~~~~~~~~~~~~~^^\n",
60-
" File \"/Users/lmandel/.pyenv/versions/3.13.0/lib/python3.13/asyncio/selector_events.py\", line 1202, in close\n",
61-
" super().close()\n",
62-
" ~~~~~~~~~~~~~^^\n",
63-
" File \"/Users/lmandel/.pyenv/versions/3.13.0/lib/python3.13/asyncio/selector_events.py\", line 865, in close\n",
64-
" self._loop.call_soon(self._call_connection_lost, None)\n",
65-
" ~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
66-
" File \"/Users/lmandel/.pyenv/versions/3.13.0/lib/python3.13/asyncio/base_events.py\", line 829, in call_soon\n",
67-
" self._check_closed()\n",
68-
" ~~~~~~~~~~~~~~~~~~^^\n",
69-
" File \"/Users/lmandel/.pyenv/versions/3.13.0/lib/python3.13/asyncio/base_events.py\", line 552, in _check_closed\n",
70-
" raise RuntimeError('Event loop is closed')\n",
71-
"RuntimeError: Event loop is closed\n"
72-
]
73-
}
74-
],
34+
"outputs": [],
7535
"source": [
7636
"%load_ext pdl.pdl_notebook_ext"
7737
]
@@ -90,19 +50,10 @@
9050
},
9151
{
9252
"cell_type": "code",
93-
"execution_count": 2,
53+
"execution_count": null,
9454
"id": "f3c62df1-0347-4711-acd7-3892cfd5df30",
9555
"metadata": {},
96-
"outputs": [
97-
{
98-
"name": "stdout",
99-
"output_type": "stream",
100-
"text": [
101-
"Hello!\n",
102-
"\u001b[32mHello! It seems like we're having a bit of an interruption in time due to the knowledge cutoff date being April 2024 and today's date being March 26, 2025. As Granite, I can still provide information based on knowledge up to that point, so go ahead with your question! How can I assist you within this context?\u001b[0m"
103-
]
104-
}
105-
],
56+
"outputs": [],
10657
"source": [
10758
"%%pdl --reset-context\n",
10859
"text:\n",
@@ -124,76 +75,10 @@
12475
},
12576
{
12677
"cell_type": "code",
127-
"execution_count": 3,
78+
"execution_count": null,
12879
"id": "bb01f89d-afaa-409c-ad48-10cc50c3fbc5",
12980
"metadata": {},
130-
"outputs": [
131-
{
132-
"name": "stdout",
133-
"output_type": "stream",
134-
"text": [
135-
"Find the fastest way for a seller to visit all the cities in their region\n",
136-
">> Response:\n",
137-
"\u001b[32mTo determine the fastest route for a seller to visit every city in their region, we employ solutions from advanced graph theory and optimization algorithms. Here’s how you could approach this problem using modern computational methods:\n",
138-
"\n",
139-
"1. **Data Representation**:\n",
140-
" - Begin by compiling all cities of interest that constitute your seller's region into a set of vertices or nodes in an undirected graph (G = (V, E)), where each vertex 'v' represents one city.\n",
141-
" - Assign weights to the edges (w(u, v)) based on a suitable metric relevant to your seller’s context – this could be driving distance between cities measured via GPS if vehicles are used for travel, or it might represent time estimations for business travelers if you're optimizing for operational efficiency.\n",
142-
"\n",
143-
"2. **Algorithm Selection**: For solving the complicated TSP problem of finding the shortest path through all these points while minimizing total cost (distance, time taken, etc.), consider one of the following proven methods:\n",
144-
" \n",
145-
" - **Exact Methods**:\n",
146-
" - **Christofides' Algorithm** – This is a widely accepted approximation algorithm with a guaranteed performance bound of 3/2 times the optimal solution. It converts the TSP into a combination of a near-optimal spanning tree and certain Hamiltonian cycles, making it computationally managed for most realistic scenarios.\n",
147-
" - **Genetic Algorithms**, Ant Colony Optimization, or other heuristics – These evolutionary methods are powerful approximators that draw on principles from biological processes to iteratively refine solutions until good quality is reached rapidly.\n",
148-
" \n",
149-
" - **Commercial and Open-Source TSP Solvers**: \n",
150-
" Tools like CPLEX (IBM's solvers) for complex optimization tasks can efficiently tackle larger TSP problems. For open-source solutions, GraphHopper or Google OR-Tools provide robust and scalable alternatives, though they might demand a more powerful computational platform than smartphone-level hardware would suggest.\n",
151-
"\n",
152-
"3. **Implementation Steps**:\n",
153-
" - **Data Preparation**: Compile the list of cities with associated cost data into the correct format for your chosen algorithm – typically, a matrix where w(cityi, cityj) equals the cost or distance between them.\n",
154-
" \n",
155-
" - **Execution**:\n",
156-
" - If using an exact method like Christofides', implement this process iteratively:\n",
157-
" 1. Start with an initial spanning tree and two or more Hamiltonian cycles.\n",
158-
" 2. At each step, replace a part of the minimum-weight alternating path from the tree with the edges from one of these cycles (to enhance the overall tour quality).\n",
159-
" 3. When no further improvements are possible in this manner, you have an approximation to the optimum solution.\n",
160-
" - For heuristics or exact solvers like CPLEX:\n",
161-
" 1. Load your dataset into the solver software.\n",
162-
" 2. Set objective parameters to minimize \"total cost\" (e.g., time or distance).\n",
163-
" 3. Run the solver and wait for the optimal (or near-optimal) route solution to present itself.\n",
164-
"\n",
165-
"4. **Analysis**:\n",
166-
" - Analyze outcomes from both exact and heuristic methods. Compare results to qualitatively assess which approach gives a more efficient route for your seller, considering tolerance levels for deviations from theoretical optima due to computational restrictions or other constraints on your data. Adjust parameters like the \"population size\" in genetic algorithms or solver settings like time limits if needed, recalculating until satisfactory performance is achieved.\n",
167-
"\n",
168-
"5. **Route Interpretation**:\n",
169-
" - Once a solution is secured:\n",
170-
" - Map out each city it includes.\n",
171-
" - Estimate travel times between them according to the weight function used (actual transit times, expected average driving speeds, business-optimal schedules). \n",
172-
" - Use this insight to plan trips efficiently, maximizing sales visits while minimizing operational downtime or travel expenses.\n",
173-
"\n",
174-
"Ultimately, leveraging these advanced analytical tools and following a structured procedure ensures that the seller not only completes all necessary city visits but also does so in the most efficient manner possible based on the constraints and objectives defined for this strategic route planning problem.\u001b[0m>> Thoughts:\n",
175-
"1. **Understanding the Problem**: The goal is to find the most efficient route that allows a seller to visit every city in their defined region exactly once. This problem can be solved using strategies from graph theory, specifically, the Traveling Salesman Problem (TSP), a well-known type of optimization problem.\n",
176-
"\n",
177-
"2. **Representation**: Cities can be represented as vertices in a mathematical graph. Edges between these vertices would represent potential routes that connect cities, with weights possibly measuring distance, time, or cost depending on the context.\n",
178-
"\n",
179-
"3. **Optimal Solution**: The ideal solution for this problem is an optimal TSP tour – a route visiting each city exactly once and returning to the initial city, minimizing total travel distance, time, or cost.\n",
180-
"\n",
181-
"4. **Algorithms and Tools**: Several algorithms can solve TSP efficiently:\n",
182-
" - ** brute force method** – This could involve generating every possible permutation of city visits then selecting the one with the shortest cumulative distance or cost. Though practical for small numbers of cities due to exponential time complexity (O(n!)), it's impractical for larger problems like global routes.\n",
183-
" - **approximation algorithms** – Such as Christofides' algorithm or a variant of the nearest neighbor (NN) approach can quickly produce near-optimal solutions with provable performance bounds.\n",
184-
" - **Exact solvers** including commercial software packages (like CPLEX, Gurobi) and open-source tools (such as BranchAndCut in Python's PuLP library), which are capable of solving larger TSP problems to global optimality but at the cost of computational time proportional to 2^n or n log(n).\n",
185-
"\n",
186-
"5. **Implementation Considerations**:\n",
187-
" - **Input Data**: Necessary data should be provided including a list of all cities and their coordinates (if route calculations depend on geographical distance) along with connectivity information between these points.\n",
188-
" - **Software Tools** – Depending upon the city count, a laptop might suffice for exploring optimal routes with current techniques; professional TSP tools could be required for larger datasets or if global optima beyond approximations are needed.\n",
189-
"\n",
190-
"6. **Solution Outline**:\n",
191-
" 1. Collect and record all cities in the seller's region as vertices of a graph.\n",
192-
" 2. Weigh each city pair by its relevant metric (distance, time, cost), constituting edges or distances within this graph.\n",
193-
" 3. Employ an optimal TSP solver tool/algorithm to compute the least aggregate distance path visiting all cities once and returning to the origin.\n"
194-
]
195-
}
196-
],
81+
"outputs": [],
19782
"source": [
19883
"%%pdl --reset-context\n",
19984
"text:\n",
@@ -222,18 +107,10 @@
222107
},
223108
{
224109
"cell_type": "code",
225-
"execution_count": 4,
110+
"execution_count": null,
226111
"id": "d7149b3f",
227112
"metadata": {},
228-
"outputs": [
229-
{
230-
"name": "stdout",
231-
"output_type": "stream",
232-
"text": [
233-
"Did Faith Hill take a break from recording after releasing her second album, It Matters to Me?Error during 'granite3.2:2b' model call: PDLRuntimeError(\"Error during 'granite3.2:2b' model call: ValueError('Failed to parse citations, documents and hallucinations from model ouput.')\")\n"
234-
]
235-
}
236-
],
113+
"outputs": [],
237114
"source": [
238115
"%%pdl --reset-context\n",
239116
"defs:\n",
@@ -285,7 +162,25 @@
285162
" controls:\n",
286163
" hallucinations: true\n",
287164
" citations: true\n",
288-
" "
165+
" modelResponse: output\n",
166+
"- \"\\nHallucinations:\\n\"\n",
167+
"- for: \n",
168+
" hallucination: ${ output.results[0].next_message.hallucinations }\n",
169+
" repeat:\n",
170+
" text: \n",
171+
" - \"Hallucination Risk: ${ hallucination.risk }\"\n",
172+
" - \"\\nSentence: ${ hallucination.response_text }\"\n",
173+
" join: \n",
174+
" with: \"\\n\"\n",
175+
"- \"\\n\\nCitations:\\n\"\n",
176+
"- for:\n",
177+
" citation: ${ output.results[0].next_message.citations }\n",
178+
" repeat:\n",
179+
" text: \n",
180+
" - \"Citation: ${ citation.context_text }\"\n",
181+
" - \"\\nSentence: ${ citation.response_text }\"\n",
182+
" join:\n",
183+
" with: \"\\n\""
289184
]
290185
},
291186
{
@@ -311,7 +206,7 @@
311206
],
312207
"metadata": {
313208
"kernelspec": {
314-
"display_name": "pdl-3.13",
209+
"display_name": "Python 3 (ipykernel)",
315210
"language": "python",
316211
"name": "python3"
317212
},
@@ -325,7 +220,7 @@
325220
"name": "python",
326221
"nbconvert_exporter": "python",
327222
"pygments_lexer": "ipython3",
328-
"version": "3.13.0"
223+
"version": "3.12.5"
329224
}
330225
},
331226
"nbformat": 4,

0 commit comments

Comments
 (0)