Skip to content

Commit a64d3a3

Browse files
authored
Add further examples (#13)
1 parent 68d1319 commit a64d3a3

File tree

3 files changed

+60
-0
lines changed

3 files changed

+60
-0
lines changed

examples/structured-response.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/usr/bin/env python
2+
"""Example script demonstrating an interactive LLM chatbot."""
3+
4+
import json
5+
6+
import lmstudio as lm
7+
8+
class BookSchema(lm.BaseModel):
9+
"""Structured information about a published book."""
10+
title: str
11+
author: str
12+
year: int
13+
14+
model = lm.llm()
15+
16+
result = model.respond("Tell me about The Hobbit", response_format=BookSchema)
17+
book = result.parsed
18+
19+
print(json.dumps(book, indent=2))

examples/tool-use-multiple.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!/usr/bin/env python
2+
"""Example script demonstrating agent use of multiple tools."""
3+
4+
import math
5+
import lmstudio as lm
6+
7+
def add(a: int, b: int) -> int:
8+
"""Given two numbers a and b, returns the sum of them."""
9+
return a + b
10+
11+
def is_prime(n: int) -> bool:
12+
"""Given a number n, returns True if n is a prime number."""
13+
if n < 2:
14+
return False
15+
sqrt = int(math.sqrt(n))
16+
for i in range(2, sqrt):
17+
if n % i == 0:
18+
return False
19+
return True
20+
21+
model = lm.llm("qwen2.5-7b-instruct")
22+
model.act(
23+
"Is the result of 12345 + 45668 a prime? Think step by step.",
24+
[add, is_prime],
25+
on_message=print,
26+
)

examples/tool-use.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/usr/bin/env python
2+
"""Example script demonstrating agent tool use."""
3+
4+
import lmstudio as lm
5+
6+
def multiply(a: float, b: float) -> float:
7+
"""Given two numbers a and b. Returns the product of them."""
8+
return a * b
9+
10+
model = lm.llm("qwen2.5-7b-instruct")
11+
model.act(
12+
"What is the result of 12345 multiplied by 54321?",
13+
[multiply],
14+
on_message=print,
15+
)

0 commit comments

Comments
 (0)