1+ import synalinks
2+ import asyncio
3+
4+ class Query (synalinks .DataModel ):
5+ query : str
6+
7+ class Answer (synalinks .DataModel ):
8+ answer : str
9+
10+ class AnswerWithThinking (synalinks .DataModel ):
11+ thinking : str
12+ answer : str
13+
14+ # Logical Or
15+
16+ # When a two data models are provided, the logical or perform a concatenation
17+ # of the two data models. However when given a `None`, it ignore it to give
18+ # you the one that isn't None.
19+
20+ # This behavior can be summarized in the following truth table:
21+
22+ # Truth Table:
23+
24+ # | `x1` | `x2` | Logical Or (`|`) |
25+ # | ------ | ------ | ---------------- |
26+ # | `x1` | `x2` | `x1 + x2` |
27+ # | `x1` | `None` | `x1` |
28+ # | `None` | `x2` | `x2` |
29+ # | `None` | `None` | `None` |
30+
31+ answer = Answer (answer = "Toulouse" ) | None
32+
33+ print (answer .prettify_json ())
34+ # {
35+ # "answer": "Toulouse"
36+ # }
37+
38+ answer = None | AnswerWithThinking (
39+ thinking =
40+ (
41+ "LAAS CNRS (Laboratoire d'Analyse et d'Architecture des Systèmes) is located in "
42+ "Toulouse and is renowned for its research in robotics."
43+ " Toulouse is also widely recognized as a central hub for aeronautics and"
44+ " space in Europe. It houses the headquarters of Airbus and several "
45+ "important aerospace research centers. and aeronautics."
46+ ),
47+ answer = "Toulouse" )
48+
49+ print (answer .prettify_json ())
50+ # {
51+ # "thinking": "LAAS CNRS (Laboratoire d'Analyse et d'Architecture des
52+ # Syst\u00e8mes) is located in Toulouse and is renowned for its research
53+ # in robotics. Toulouse is also widely recognized as a central hub for
54+ # aeronautics and space in Europe. It houses the headquarters of Airbus
55+ # and several important aerospace research centers. and aeronautics.",
56+ # "answer": "Toulouse"
57+ # }
58+
59+ # Why is that useful ? Let's explain it with an example,
60+ # imagine you want an adaptative system that is able to
61+ # answer shortly, or take more time to "think" before answering
62+ # depending on the question difficulty.
63+ #
64+ # Example:
65+
66+ async def main ():
67+ language_model = synalinks .LanguageModel (model = "ollama/mistral" )
68+
69+ inputs = synalinks .Input (data_model = Query )
70+ answer_without_thinking , answer_with_thinking = await synalinks .Branch (
71+ question = "Evaluate the difficulty of the query" ,
72+ labels = ["easy" , "difficult" ],
73+ branches = [
74+ synalinks .Generator (
75+ data_model = Answer ,
76+ language_model = language_model ,
77+ ),
78+ synalinks .Generator (
79+ data_model = AnswerWithThinking ,
80+ language_model = language_model ,
81+ )
82+ ],
83+ language_model = language_model ,
84+ # We can optionally return the decision,
85+ # in Synalinks there is no black-box component!
86+ # Every LM inference, can be returned
87+ # for evaluation or explainability
88+ return_decision = False ,
89+ )(inputs )
90+
91+ # The outputs is the answer without thinking OR the answer with thinking
92+ outputs = answer_without_thinking | answer_with_thinking
93+
94+ program = synalinks .Program (
95+ inputs = inputs ,
96+ outputs = outputs ,
97+ name = "adaptative_qa" ,
98+ description = "A program that take the time to think if the query is difficult to answer"
99+ )
100+
101+ answer = await program (Query (query = "What is French city of robotics and aeronautics?" ))
102+
103+ print (answer .prettify_json ())
104+ # {
105+ # "thinking": "The answer to the given query involves finding a city in
106+ # France that is known for robotics and aeronautics. While there might be
107+ # several cities that have significant presence in these fields, Toulouse
108+ # is one of the most renowned due to the presence of well-established
109+ # institutions like EADS (European Aeronautic Defence and Space Company),
110+ # IRIT (Institut de Recherche en Informatique pour le Traitement Automatique des Images)
111+ # and LAAS CNRS (Laboratoire d'Analyse et d'Architecture des Syst\u00e8mes).",
112+ # "answer": "Toulouse"
113+ # }
114+
115+ if __name__ == "__main__" :
116+ asyncio .run (main ())
0 commit comments