-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
165 lines (118 loc) · 4.15 KB
/
Copy pathmain.py
File metadata and controls
165 lines (118 loc) · 4.15 KB
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
164
165
from pydantic import Field
from tinygent.core.datamodels.messages import TinyHumanMessage
from tinygent.core.datamodels.messages import TinySystemMessage
from tinygent.core.factory import build_llm
from tinygent.core.types import TinyLLMInput
from tinygent.core.types import TinyModel
from tinygent.tools import reasoning_tool
from tinygent.tools import tool
class AddInput(TinyModel):
a: int = Field(..., description='First number')
b: int = Field(..., description='Second number')
@tool
def add(data: AddInput) -> int:
return data.a + data.b
class CapitalizeInput(TinyModel):
text: str = Field(..., description='Text to capitalize')
@reasoning_tool
def capitalize(data: CapitalizeInput) -> str:
return data.text.upper()
class SummaryResponse(TinyModel):
summary: str
def count_tokens():
llm = build_llm('openai:gpt-4o-mini')
messages = [
TinySystemMessage(content='You are helpful tiny assistant.'),
TinyHumanMessage(content='Tell me a joke about programmers.'),
]
result = llm.count_tokens_in_messages(
messages=messages,
)
print(f'[NUMBER OF TOKENS] {result} for {"\n".join([m.tiny_str for m in messages])}')
def basic_generation():
llm = build_llm('openai:gpt-4o-mini')
result = llm.generate_text(
llm_input=TinyLLMInput(
messages=[TinyHumanMessage(content='Tell me a joke about programmers.')]
)
)
for msg in result.tiny_iter():
print(f'[BASIC TEXT GENERATION] {msg}')
print(f'[TEXT GENERATION - to_string()] {result.to_string()}')
def structured_generation():
llm = build_llm('openai:gpt-4o-mini')
result = llm.generate_structured(
llm_input=TinyLLMInput(
messages=[
TinyHumanMessage(
content='Summarize why the sky is blue in one sentence.'
)
],
),
output_schema=SummaryResponse,
)
print(f'[STRUCTURED RESULT] {result.summary}')
def generation_with_tools():
llm = build_llm('openai:gpt-4o-mini')
tools_list = [add, capitalize]
tools = {tool.info.name: tool for tool in tools_list}
result = llm.generate_with_tools(
llm_input=TinyLLMInput(
messages=[
TinyHumanMessage(
content='Capitalize "tinygent is powerful". Then add 5 and 7.'
)
]
),
tools=tools_list,
)
for message in result.tiny_iter():
if message.type == 'chat':
print(f'[LLM RESPONSE] {message.content}')
elif message.type == 'tool':
output = tools[message.tool_name](**message.arguments)
print(f'[TOOL CALL] {message.tool_name}({message.arguments}) => {output}')
async def async_generation():
llm = build_llm('openai:gpt-4o-mini')
result = await llm.agenerate_text(
llm_input=TinyLLMInput(
messages=[TinyHumanMessage(content='Name three uses of AI in medicine.')]
)
)
for msg in result.tiny_iter():
print(f'[ASYNC TEXT GENERATION] {msg}')
async def text_streaming():
llm = build_llm('openai:gpt-4o-mini')
async for chunk in llm.stream_text(
llm_input=TinyLLMInput(
messages=[TinyHumanMessage(content='Tell me a joke about programmers.')]
)
):
if chunk.is_message:
assert chunk.message is not None
print(f'[STREAMED CHUNK] {chunk.message.content}')
async def tool_call_streaming():
llm = build_llm('openai:gpt-4o-mini')
tools = [add, capitalize]
async for chunk in llm.stream_with_tools(
llm_input=TinyLLMInput(
messages=[
TinyHumanMessage(
content='Capitalize "tinygent is powerful". Then add 5 and 7.'
)
]
),
tools=tools,
):
print(f'[STREAMED CHUNK] {chunk}')
if __name__ == '__main__':
async def main():
count_tokens()
basic_generation()
structured_generation()
generation_with_tools()
await async_generation()
await text_streaming()
await tool_call_streaming()
import asyncio
asyncio.run(main())