diff --git a/examples/structured-response.py b/examples/structured-response.py new file mode 100755 index 0000000..8714ef6 --- /dev/null +++ b/examples/structured-response.py @@ -0,0 +1,19 @@ +#!/usr/bin/env python +"""Example script demonstrating an interactive LLM chatbot.""" + +import json + +import lmstudio as lm + +class BookSchema(lm.BaseModel): + """Structured information about a published book.""" + title: str + author: str + year: int + +model = lm.llm() + +result = model.respond("Tell me about The Hobbit", response_format=BookSchema) +book = result.parsed + +print(json.dumps(book, indent=2)) diff --git a/examples/tool-use-multiple.py b/examples/tool-use-multiple.py new file mode 100644 index 0000000..0901141 --- /dev/null +++ b/examples/tool-use-multiple.py @@ -0,0 +1,26 @@ +#!/usr/bin/env python +"""Example script demonstrating agent use of multiple tools.""" + +import math +import lmstudio as lm + +def add(a: int, b: int) -> int: + """Given two numbers a and b, returns the sum of them.""" + return a + b + +def is_prime(n: int) -> bool: + """Given a number n, returns True if n is a prime number.""" + if n < 2: + return False + sqrt = int(math.sqrt(n)) + for i in range(2, sqrt): + if n % i == 0: + return False + return True + +model = lm.llm("qwen2.5-7b-instruct") +model.act( + "Is the result of 12345 + 45668 a prime? Think step by step.", + [add, is_prime], + on_message=print, +) diff --git a/examples/tool-use.py b/examples/tool-use.py new file mode 100755 index 0000000..adb1422 --- /dev/null +++ b/examples/tool-use.py @@ -0,0 +1,15 @@ +#!/usr/bin/env python +"""Example script demonstrating agent tool use.""" + +import lmstudio as lm + +def multiply(a: float, b: float) -> float: + """Given two numbers a and b. Returns the product of them.""" + return a * b + +model = lm.llm("qwen2.5-7b-instruct") +model.act( + "What is the result of 12345 multiplied by 54321?", + [multiply], + on_message=print, +)