AI Engineering (4)
在Google colab里调用AI辅助
Google colab 是一个很好的平台,我们可以在里面import pytorch工具包来帮助我们做ML和DL的任务,以及大模型的训练
首先我们可以在colab里面调用ai辅助
Getting_started_with_google_colab_ai.ipynb - Colab
它有如下功能:
- Generate text 生成文字
- Translate languages 翻译
- Write creative content 写创意内容
- Categorize text 给文本分类
如何调用呢?
首先我们查看有哪些ai可供调用:
#python
form google.colab import ai
ai.list_models()
输出:
['google/gemini-2.5-flash', 'google/gemini-2.5-flash-lite']
当然我没交钱只有这些轻量化model可以用,交了钱就可以使用Gemini的最好版本
Pro: These are the most capable models, ideal for complex reasoning, creative tasks, and detailed analysis.
Flash: These models are optimized for high speed and efficiency, making them great for summarization, chat applications, and tasks requiring rapid responses.
Gemma: These are lightweight, open-weight models suitable for a variety of text generation tasks and are great for experimentation.
接下来我们来选择调用哪个模型
#python
form google.colab import ai
response = ai.generate_text("What is the lead vocalist of the band MyGO!!!!!", model_name = "google/gemini-2.0-flash-lite")
print(response)
输出
The lead vocalist of the band MyGO!!!!! is **Tomori Takamatsu (高松 燈)**.
She is voiced by **Hina Youmiya (羊宮 妃那)**.
如果出现以下报错
InternalServerError: Error code: 503 - {'message': 'The requested model is currently unavailable.', 'type': 'server_error'}
这里报错是503错误,即基础设施问题,我们可以稍等一段时间再运行,或者运行以下代码:
#python
# @title Simple batch generation example
# Only text-to-text input/output is supported
from google.colab import ai
response = ai.generate_text("What is the lead vocalist of the band MyGO!!!!!")
print(response)
也可以输出相同的结果
对于长文本生成,我们可以使用流式传输响应,即逐token显示输出,而不是等待整个响应完成,以提供更交互和响应式体验
什么是流式传输响应?
我们可以用stream = True,启用流式传输模式,让AI一边生成内容一边把刚生成的token实时推送给用户,而不是等待所有内容全部生成完毕后再一次性返回
# @title Simple streaming example
from google.colab import ai
stream = ai.generate_text("Tell me a short story.", stream=True)
for text in stream:
print(text, end='')
于是会有这样的输出:
The old library smelled of dust and forgotten dreams, a scent Elara had come to cherish over her forty years as head librarian. Now, its towering shelves, once vibrant with the murmur of readers, stood silent, awaiting the movers who would dismantle its history.
Elara ran a gloved hand over a worn copy of "Wuthering Heights," a sigh escaping her lips. She was tasked with the final inventory, a bittersweet chore that felt more like a eulogy. As she reached for a slim, leather-bound volume perched high on an obscure shelf – one she'd never quite noticed before – her fingers brushed against something loose.
Behind the book, not part of the shelf itself, was a small, ornate wooden panel. Curiosity, a feeling she thought had dulled with age, flared within her. She nudged it. With a soft click, it slid inwards, revealing a small, dark recess.
Inside lay a single object: a music box. It was crafted from dark, polished wood, intricately carved with miniature books and swirling vines. There was no key, no obvious winding mechanism. Intrigued, Elara picked it up. It felt warm to the touch, almost alive.
As her thumb traced the delicate carvings, a faint, almost imperceptible tremor ran through the box. Then, from within, a single, clear note chimed. It was followed by another, and another, weaving into a melody Elara had never heard, yet somehow felt she'd always known. It was like the rustling of turning pages, the whispered secrets of fictional lovers, the triumphant fanfare of heroes, all distilled into pure sound.
The air in the library shimmered. Dust motes, caught in the slivers of afternoon sun, seemed to dance with newfound energy. The books on the shelves around her, thousands of them, appeared to hum with a quiet energy. Elara could almost hear the faint murmur of stories unfolding within their covers, a collective breath taken by every character from every tale ever told.
A tear traced a path down her cheek. It wasn't a tear of sadness for the closing library, but of profound wonder. This wasn't just a music box; it was the library's heart, its soul, made manifest. It was the whisper of every author, the dream of every reader.
She held the box close, the ethereal music weaving around her. The movers would arrive tomorrow, but they wouldn't find this. This melody, this secret life of the library, would go with her. The old building might close its doors, but its stories, its magic, would continue to sing, held safe within the small, warm music box, and forever in Elara's heart.
但是有时候输出会出现这种情况:
"Hello"
","
" world"
"!"
" This"
" is"
" a"
" very"
" long"
...
为了文本美观,我们可以用这个LineWrapper在终端里实时、美观地把输出的碎片token拼接并自动换行
#@title Text formatting setup
#code is not necessary for colab.ai, but is useful in fomatting text chunks
import sys
class LineWrapper:
def __init__(self, max_length=80):
self.max_length = max_length
self.current_line_length = 0
def print(self, text_chunk):
i = 0
n = len(text_chunk)
while i < n:
start_index = i
while i < n and text_chunk[i] not in ' \n': # Find end of word
i += 1
current_word = text_chunk[start_index:i]
delimiter = ""
if i < n: # If not end of chunk, we found a delimiter
delimiter = text_chunk[i]
i += 1 # Consume delimiter
if current_word:
needs_leading_space = (self.current_line_length > 0)
# Case 1: Word itself is too long for a line (must be broken)
if len(current_word) > self.max_length:
if needs_leading_space: # Newline if current line has content
sys.stdout.write('\n')
self.current_line_length = 0
for char_val in current_word: # Break the long word
if self.current_line_length >= self.max_length:
sys.stdout.write('\n')
self.current_line_length = 0
sys.stdout.write(char_val)
self.current_line_length += 1
# Case 2: Word doesn't fit on current line (print on new line)
elif self.current_line_length + (1 if needs_leading_space else 0) + len(current_word) > self.max_length:
sys.stdout.write('\n')
sys.stdout.write(current_word)
self.current_line_length = len(current_word)
# Case 3: Word fits on current line
else:
if needs_leading_space:
# Define punctuation that should not have a leading space
# when they form an entire "word" (token) following another word.
no_leading_space_punctuation = {
",", ".", ";", ":", "!", "?", # Standard sentence punctuation
")", "]", "}", # Closing brackets
"'s", "'S", "'re", "'RE", "'ve", "'VE", # Common contractions
"'m", "'M", "'ll", "'LL", "'d", "'D",
"n't", "N'T",
"...", "…" # Ellipses
}
if current_word not in no_leading_space_punctuation:
sys.stdout.write(' ')
self.current_line_length += 1
sys.stdout.write(current_word)
self.current_line_length += len(current_word)
if delimiter == '\n':
sys.stdout.write('\n')
self.current_line_length = 0
elif delimiter == ' ':
# If line is full and a space delimiter arrives, it implies a wrap.
if self.current_line_length >= self.max_length:
sys.stdout.write('\n')
self.current_line_length = 0
sys.stdout.flush()
我们来把定义的这个类用到生成文本里面
# @title Formatted streaming example
from google.colab import ai
wrapper = LineWrapper()
for chunk in ai.generate_text('Give me a long winded description about the evolution of the Roman Empire.', stream=True):
wrapper.print(chunk)
输出的内容格式就更好
Ah, to speak of the Roman Empire's evolution is to embark upon a grand,
meandering journey through the annals of antiquity, a narrative woven from
threads of myth, conquest, ambition, and ultimately, an inexorable decline. It
is not merely a story of dates and battles, but of a civilization's protean
transformation, from a nascent settlement on a series of hills to the undisputed
master of the Mediterranean world, and finally, to a fractured behemoth
succumbing to its own vastness and external pressures.
Our odyssey begins not with an empire, but with a humble city-state, cloaked in
the m ists of legend. The tale of Romulus and Remus, suckled by a she-wolf,
speaks to a fierce, almost primeval beginning, a destiny forged in fratricide
and the will to build. For over two centuries, from the traditional founding in
753 BCE, Rome existed as a **Mon archy**. Ruled by a succession of seven kings,
some of whom were Etruscan, this period laid crucial foundational elements: the
Senate as an advisory body, the distinction between patricians and plebeians,
and early military organization. The expulsion of the tyrannical Lucius
Tarquinius Superbus in 509 BCE, spurred by the outrage over the rape of
Lucretia, marked a pivotal transition. The Romans, scarred by monarchy, vowed
never again to place absolute power in the hands of one man.
Thus was born the **Roman Republic**. This period, stretching for nearly five
centuries, is the crucible in which Rome’ s character was forged. Initially, it
was a volatile experiment in self-governance, dominated by the patrician
aristocracy. Yet, the plebeians, comprising the vast majority of the population
and the backbone of Rome's nascent military, agitated ceaselessly for their
rights. Through strikes, secessions, and political maneuvering, they gradually
won concessions: the creation of the Tribunes of the Plebs (with veto power),
the codification of laws in the Twelve Tables, and eventually, the right to hold
any office. This internal struggle, often fraught but ultimately resolved
through negotiation, instilled a deep-seated respect for law, civic duty, and a
complex system of checks and balances involving elected magistrates (Consuls,
Praetors, Aediles, Quaestors), the venerable Senate, and various citizen
assemblies.
It was during the Republic that Rome embarked upon its astonishing trajectory of
**expansion**. Initially fighting defensive wars against neighboring tribes –
Latins, Sab ines, Etruscans – Rome developed a formidable and adaptable military
machine, the legion. With each victory, they absorbed conquered peoples through
a judicious mix of alliances, partial citizenship, and direct control, slowly
unifying the Italian peninsula. The Punic Wars against Carthage, a titanic
struggle for mastery of the Western Mediterranean, were the defining conflict.
Hannibal Barca's audacious invasion and devastating victories pushed Rome to the
brink, yet its resilience, its deep reserves of manpower, and its unwavering
commitment to ultimate victory ultimately prevailed. Carthage was destroyed, and
Rome emerged as the sole superpower of the Mediterranean.
This ascendancy, however, came at a steep cost. The influx of wealth, slaves,
and vast provincial territories began to unravel the fabric of the Republic.
Small independent farmers, the traditional backbone of the army, lost their
lands to wealthy aristocrats operating large slave-run estates (latifundia),
swelling the ranks of the urban poor. Corruption became endemic in provincial
administration. The Gracchi brothers, Tiberius and Gaius, attempted land reforms
to address these injustices but were met with violent opposition, signaling a
dangerous precedent of political assassinations.
The late Republic dissolved into a century of **civil wars and profound
instability**. Generals like Marius professionalized the army, shifting
soldiers' loyalty from the state to their commanders. Sulla marched on Rome,
established a brutal dictatorship, and purged his enemies. The First Triumvirate
– Pompey, Crassus, and the magnetic Gaius Julius Caesar – attempted to share
power but ultimately fractured. Caesar’s conquest of Gaul, his defiance of the
Senate, and his crossing of the Rubicon plunged Rome into its most famous civil
war. His victory and subsequent assassination highlighted the Republic's
terminal illness: its institutions, designed for a small city-state, could no
longer govern a sprawling empire, and its leading men prioritized personal
ambition over collective stability.
The final act of the Republic' s demise played out between Caesar's adopted son,
Octavian, and his rival Mark Antony. Their conflict culminated in the Battle of
Actium in 31 BCE, a decisive victory for Octavian. Wary of meeting Caesar's
fate, Octavian, a political genius, meticulously dismantled the Republic while
feigning its restoration. He adopted the title of **Augustus** and carefully
crafted the role of *princeps* (first citizen), a position that granted him
unprecedented power while maintaining the facade of republican institutions.
This marked the birth of the **Roman Empire**, specifically the **Principate**.
Under Augustus, Rome entered its golden age, the **Pax Romana** – a period of
relative peace and prosperity lasting two centuries. Augustus meticulously
reformed the army, created a professional civil service, built roads and
infrastructure, and stabilized the frontiers. The Empire consolidated its vast
territorial gains, stretching from Britannia to Mesopotamia, from the Rhine to
the Sahara. Successive dynasties – the Julio- Claudians, the Flavians, and the
Antonines (the "Five Good Emperors") – largely maintained this stability, albeit
with moments of tyrannical excess (Nero, Domitian) or expansionist ambition
(Trajan, who brought the Empire to its greatest territorial extent). Roman law,
Latin language, engineering prowess (aqueducts, col iseums, baths), and a
pervasive Roman culture spread throughout the provinces, forging a remarkably
unified, if diverse, entity.
But no empire, however mighty, is eternal. The seeds of decline were sown even
during its zenith. The vastness of the Empire made defense increasingly
difficult and expensive. Economic disparities grew, with the urban centers
thriving at the expense of neglected rural areas. The reliance on slave labor
hindered technological innovation. A more insidious shift was the gradual
erosion of the civic virtue that had characterized the Republic; citizenship
became a universal right rather than a privileged responsibility, and the
imperial bureaucracy supplanted direct citizen participation.
The true turning point arrived in the ** Crisis of the Third Century (235-284
CE)**. A succession of "barracks emperors," often lasting mere months, seized
power through military coups, only to be assassinated by their own troops. The
Empire fractured, with secessionist states emerging in Gaul and Palmyra.
Simultaneously, devastating barbarian incursions from all sides – Goths,
Alamanni, Franks, Sassanid Persians – put immense pressure on the frontiers.
Economic collapse, rampant inflation (due to currency debasement), devastating
plagues, and a breakdown of internal order brought Rome to the brink of total
collapse.
It was **Diocletian** who pulled the Empire back from the abyss. A brilliant but
ruthless reformer, he recognized the impossibility of one man governing such a
vast territory. He established the **Tetrarchy**, dividing the Empire into East
and West, each with an Augustus and a Caesar. He dramatically increased the
army's size, reformed the bureaucracy, and attempted to stabilize the economy
with drastic price edicts. While effective, these reforms made the Empire more
authoritarian, transforming the Principate into the **Dominate**, where the
emperor was an overt monarch (dominus et deus – lord and god).
**Constantine the Great** built upon Diocletian's foundations. He reunited the
Empire under a single emperor, famously founded a new capital at Constantinople
(Byzantium), strategically located at the crossroads of Europe and Asia, and,
most profoundly, legalized Christianity with the Edict of Milan in 313 CE. This
shift to Christianity, though gradual, fundamentally altered the Empire's
identity, providing a new unifying ideology, but also creating new internal
tensions.
The division of the Empire into permanent **Eastern and Western halves** in 395
CE, though initially an administrative measure, presaged their diverging fates.
The Western Roman Empire, with its capital eventually moving from Rome to
Ravenna, faced a compounding series of existential threats. Its economy was less
robust, its frontiers more exposed, and its political stability increasingly
fragile. Continuous waves of **barbarian migrations** – Goths, Vandals, Huns
under Attila – chipped away at its territories, resources, and morale. The sack
of Rome by the Vis igoths in 410 CE, and again by the Vandals in 455 CE, were
profound psychological shocks.
The final act was a slow, agonizing whimper rather than a bang. Emperors became
increasingly powerless, mere puppets of barbarian warlords who controlled the
army. The Roman state in the West gradually ceased to function effectively.
Taxation faltered, infrastructure decayed, and central authority vanished. The
traditional date for the **Fall of the Western Roman Empire** is 476 CE, when
the Germanic chieftain Odoacer deposed the last Western Roman Emperor, Romulus
Augustulus, and sent the imperial regalia to Constantinople.
Yet, this was not an absolute end. The **Eastern Roman Empire**, or **Byzantine
Empire**, with its vibrant capital at Constantinople, continued to thrive for
another thousand years, preserving Roman law, Greek culture, and Christian
theology. It remained a beacon of civilization long after the West had
fragmented into disparate Germanic kingdoms.
The evolution of the Roman Empire is, therefore, a tapestry of contradictions: a
Republic that devoured itself, an Empire built on peace secured by war, a
civilization that reached astonishing heights only to succumb to its own weight
and the relentless tides of history. It was a journey from rural resilience to
imperial grandeur, from civic virtue to cynical ambition, from pagan polytheism
to universal monotheism. Its legacy, imprinted on our laws, languages, political
thought, architecture, and even our calendar, continues to shape the modern
world, a testament to its enduring, albeit ever-changing, greatness.
更多推荐


所有评论(0)