
Using Apple's Foundation Models in Raku Sessions
'What is the speed of a rocket leaving the Moon?' ==>
以下正文同步自 Hacker News 最新,版权归原站所有,已转换为易读排版。
use WWW::LLaMA;
'What is the speed of a rocket leaving the Moon?' ==>
llama-playground(path => 'chat/completion', model => 'system', base-url => 'http://127.0.0.1:1976', format => 'values');
The speed of a rocket leaving the Moon depends on the rocket's design and mission specifics. Generally, the escape velocity from the Moon is about 2.38 kilometers per second (approximately 8,600 kilometers per hour). However, rockets may achieve higher speeds depending on the propulsion system and mission requirements. For precise information, you would need details about the specific rocket and mission.
Try the simplest FM call (in a shell cell):
fm respond 'How many total countries in the world?'
There are 195 countries in the world.`
There are 195 countries in the world.
Consult the companion Jupyter notebook for live, executable examples: “Apple-Foundation-Models-usage-examples.ipynb”
Introduction
This document demonstrates how to use Apple’s Foundation Models (FM) in Raku sessions. It is designed for developers who want to integrate FM capabilities—such as text generation, image description, and structured data extraction—into Raku workflows, Jupyter notebooks, or computational Markdown documents.
A companion Jupyter notebook provides live, executable examples alongside this guide.
The guide is organized into the following major sections:
Raku’s computational Markdown and Jupyter notebooks support evaluation of OS shell cells, enabling the simplest FM call without any prior setup.
fm respond 'How many total countries in the world?'
There are 195 countries in the world.
qx{fm respond 'Which are most used writing systems?'}
The most widely used writing systems are:
1. Latin script – Used in English, Spanish, French, German, and many other languages.
2. Arabic script – Used in Arabic, Persian, Urdu, and other languages.
3. Chinese characters – Used in Mandarin, Cantonese, and other Chinese languages.
4. Devanagari – Used in Hindi and Nepali.
5. Bengali – Used in Bengali and Assamese.
Remark: For llama-* and llm-* subroutines the FM server must be running (fm serve). The bare fm respond command can work in isolated shell/Jupyter cells, but full integration requires the server.
Important: fm respond --image requires a local file path, not a remote URL. Download the image first (e.g., via curl -O), then pass the local path.
my $url = 'https://i.imgur.com/CStKxkw.jpeg';
shell "curl -O $url";
my proc = shell "fm respond --image {url.split('/').tail} 'Describe the image.'", :out;
$proc.out.slurp(:close)
The image shows a woman wearing a colorful dress with blue, yellow, and red geometric patterns. The dress has a high neckline and a black belt. She has red lips and her hair is styled in an updo. The background features a landscape with rolling hills, trees, and a blue sky with clouds.
- Setup
Load required packages
use WWW::LLaMA;
use WWW::LLaMA::Models;
use LLM::Functions;
use LLM::Prompts;
use Text::SubParsers;
use Data::Reshapers;
use Data::Translators;
use Data::TypeSystem;
use Math::SparseMatrix;
Usage note:
On macOS 27+ start Apple’s built‑in Foundation Model server:
fm serve
Custom port / host / socket:
fm serve --port 1976
fm serve --host 127.0.0.1 --port 1976
fm serve --socket /tmp/fm.sock
Server output:
Foundation Models Serve
access loopback-only
· POST /v1/chat/completions
· GET /v1/models
· GET /health
Model registration
The system‑recognized model names system and pcc are available in WWW::LLaMA::ver<1.0.5+>. To register additional models:
llama-known-models()<system> = True;
llama-known-models()<pcc> = True;
say llama-known-models();
llama-end-point-to-models()<chat/completions> = <gpt-3.5-turbo llamafile llama llava system pcc>;
say llama-end-point-to-models();
Note: Registration is only needed for non‑default models. The system and pcc models work out‑of‑the‑box.
Set the base URL for the FM server (once per session):
llama-base-url('http://127.0.0.1:1976')
http://127.0.0.1:1976
Chat completion with llama-playground
'What is the speed of a rocket leaving the Moon?' ==>
llama-playground(path => 'chat/completion', model => 'system', format => 'values');
The speed of a rocket leaving the Moon depends on several factors, including the rocket’s design, mission objectives, and the specifics of the lunar environment.
For a typical ascent from the lunar surface, a rocket might achieve a velocity of approximately 2,000–3,000 meters per second (about 7,200–10,800 kilometers per hour). This is a rough estimate and can vary significantly based on the mission’s requirements and the propulsion system used.
Remark: The base-url option can also be passed directly to llama-playground: llama-playground(base-url => 'http://127.0.0.1:1976', ...).
my $conf-fm = llm-configuration('LLaMA', model => 'system', base-url => 'http://127.0.0.1:1976', :max-tokens(4096), temperature => 0.4);
LLM::Configuration(:name("llama"), :model("system"), :module("WWW::LLaMA"), :max-tokens(4096))
Simple LLM generation requests
Query: “How many people live in Peru?”
llm-synthesize([
'How many people live in Peru?',
], e => $conf-fm);
Approximately 33 million people live in Peru.
Raku code generation (note: current FM output for code tasks is approximate):
llm-synthesize([
llm-prompt('CodeWriterX')('Raku'),
'Iterate over a list of random reals.',
], e => $conf-fm);
```rakudo
@rand(0..1000, 100).each { say $_ }
```
Complex queries
FM produces reliable results for simple factual queries. For cross‑domain or structured tasks, enrich prompts and use post‑processing.
my &ctFunc =
llm-function( -> $what {[
"Cross-tabulate $what and their time zones.",
'Give a JSON array of dictionaries, all having the same keys and at least one being "time-zone".',
'Please, use short names for the time zones.',
'Numeric results should be computable,',
'Do it quickly -- do not overthink it.',
llm-prompt('NothingElse')('JSON')
]},
e => $conf-fm,
form => sub-parser('JSON'):drop);
LLM::Function(-> **@args, *%args { #`(Block|3207846872152) ... }, 'llama')
my @data = |&ctFunc('Top 10 largest by area countries');
deduce-type(@data);
Vector(Struct([area, country, time-zone], [Int, Str, Str]), 10)
Approximate results: FM outputs for complex queries (tabulation, code generation, natural‑language tasks) are inherently approximate. Validate results against authoritative sources when precision is critical, and use post‑processing (JSON parsing, cross-tabulate, unit conversion) to improve reliability.
Here is the cross‑tabulation using cross-tabulate of “Data::Reshapers” given as a sparse matrix (see “Math::SparseMatrix”):
正文由 探How 从来源站点 RSS 同步,内容未经改写;遇到排版缺失或需要图片、视频时请以原文为准。