-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfm.py
More file actions
109 lines (81 loc) · 2.37 KB
/
Copy pathfm.py
File metadata and controls
109 lines (81 loc) · 2.37 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
#!/usr/bin/env python3
"""fm - Interactive terminal chat for Apple's built-in Foundation Model."""
import asyncio
import sys
import apple_fm_sdk as fm
VERSION = "0.1.0"
HELP_TEXT = """\
fm
Interactive terminal chat for Apple's built-in Foundation Model.
Usage:
fm
fm --help
fm --version
Commands in the REPL:
:help Show REPL help
:reset Start a fresh chat session
:quit Exit the program"""
def repl_help() -> str:
return HELP_TEXT.split("\n\n")[-1]
def check_availability() -> bool:
model = fm.SystemLanguageModel()
is_available, reason = model.is_available()
if is_available:
return True
print(f"Foundation Model is unavailable: {reason}", file=sys.stderr)
return False
def make_session() -> fm.LanguageModelSession:
return fm.LanguageModelSession(
instructions=(
"You are a concise terminal assistant. "
"Keep responses practical and compact unless the user asks for detail."
),
)
async def repl() -> None:
session = make_session()
while True:
try:
line = input("> ")
except (EOFError, KeyboardInterrupt):
print()
break
prompt = line.strip()
if not prompt:
continue
if prompt in (":quit", ":exit"):
break
if prompt == ":help":
print(repl_help())
continue
if prompt == ":reset":
session = make_session()
print("Conversation reset.")
continue
try:
response = await session.respond(prompt)
print(f"\n{response}\n")
except Exception as e:
print(f"Generation failed: {e}", file=sys.stderr)
def main() -> None:
args = sys.argv[1:]
if args:
match args[0]:
case "--help" | "-h":
print(HELP_TEXT)
return
case "--version" | "-v":
print(VERSION)
return
case other:
print(f"Unknown argument: {other}\n", file=sys.stderr)
print(HELP_TEXT)
sys.exit(2)
if not check_availability():
sys.exit(1)
print("Apple Foundation Model REPL")
print("Type a prompt and press return.")
print("Commands: :help, :reset, :quit")
print()
asyncio.run(repl())
if __name__ == "__main__":
main()