Skip to content

Instantly share code, notes, and snippets.

@claytantor
Last active June 27, 2025 04:44
Show Gist options
  • Select an option

  • Save claytantor/2540df4051ad00da2fe2274c6d3a68a5 to your computer and use it in GitHub Desktop.

Select an option

Save claytantor/2540df4051ad00da2fe2274c6d3a68a5 to your computer and use it in GitHub Desktop.
lmstudio-python first attempts at a Agent with Pydantic AI
class LMStudioResponse(BaseModel):
"""Model for LM Studio response"""
content: str = Field(..., description="The content of the response")
class LMStudioAgent:
model:lms.LLM = None
def __init__(
self,
deps_type: Type[BaseModel],
result_type: Type[BaseModel],
system_prompt: str,
model: lms.LLM,
):
self.model = model
self.system_prompt = system_prompt
def execute(self, input_data: Any) -> BaseModel:
prompt = (
f"{self.system_prompt} The customer provided the number {input_data}. "
"Use the `roulette_wheel` function to see if the customer has won."
)
response:lms.PredictionResult = self.model.complete(prompt, config={"maxTokens": 100})
response_content = response.content.strip()
print(f"Response content: {response_content}")
# Assuming the response is in JSON format
execute_response = LMStudioResponse(content=response_content)
return execute_response
import lmstudio as lms
client = lms.Client(api_host="simbi.local:1234")
# [LLM(identifier='mistralai_mistral-small-3.1-24b-instruct-2503')]
models:Sequence[lms.LLM] = client.list_loaded_models()
mistral:lms.LLM = None
for model in models:
model: lms.LLM
if model.identifier == "mistralai_mistral-small-3.1-24b-instruct-2503":
mistral = model
break
# Create your roulette agent with LM Studio
roulette_agent = LMStudioAgent(
model=mistral, # Custom LM Studio prefix
deps_type=int,
result_type=bool,
system_prompt=(
'Use the `roulette_wheel` function to see if the '
'customer has won based on the number they provide.'
)
)
# Example usage of the agent
def main():
print("Roulette agent is ready to use.")
# Usage remains identical to your original example
result = roulette_agent.execute(17)
print(f"Roulette result: {result}")
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment