Skip to content

Commit

Permalink
Add docs example scripts to repo (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
ncoghlan authored Feb 27, 2025
1 parent eec707a commit 400b276
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
27 changes: 27 additions & 0 deletions examples/chatbot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/usr/bin/env python
"""Example script demonstrating an interactive LLM chatbot."""

import readline # Enables input line editing

import lmstudio as lm

model = lm.llm()
chat = lm.Chat("You are a task focused AI assistant")

while True:
try:
user_input = input("You (leave blank to exit): ")
except EOFError:
print()
break
if not user_input:
break
chat.add_user_message(user_input)
prediction_stream = model.respond_stream(
chat,
on_message=chat.append,
)
print("Bot: ", end="", flush=True)
for fragment in prediction_stream:
print(fragment.content, end="", flush=True)
print()
28 changes: 28 additions & 0 deletions examples/terminal-sim.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#!/usr/bin/env python
"""Example script demonstrating a simulated terminal command processor."""

import readline # Enables input line editing

import lmstudio as lm

model = lm.llm()
console_history = []

while True:
try:
user_command = input("$ ")
except EOFError:
print()
break
if user_command.strip() == "exit":
break
console_history.append(f"$ {user_command}")
history_prompt = "\n".join(console_history)
prediction_stream = model.complete_stream(
history_prompt,
config={ "stopStrings": ["$"] },
)
for fragment in prediction_stream:
print(fragment.content, end="", flush=True)
print()
console_history.append(prediction_stream.result().content)

0 comments on commit 400b276

Please sign in to comment.