mirror of
https://github.com/zenlm/engine.git
synced 2026-10-30 15:00:05 +00:00
* Add model * Update loader * Fix llama config usage * Docs * Fix config no_rope_layers * Fix tie_word_embeddings default * Add chat template * Embed the chat templates * Fix embedding template * enable_thinking default true * Update examples * XML tools for smollm3 * Add smollm3 docs * Fix openai examples * Clippy --------- Co-authored-by: Eric Buehler <[email protected]>
72 lines
1.8 KiB
Python
72 lines
1.8 KiB
Python
import json
|
|
import os
|
|
from mistralrs import (
|
|
Runner,
|
|
Which,
|
|
ChatCompletionRequest,
|
|
Architecture,
|
|
ToolChoice,
|
|
)
|
|
|
|
|
|
def local_search(query: str):
|
|
results = []
|
|
for root, _, files in os.walk("."):
|
|
for f in files:
|
|
if query in f:
|
|
path = os.path.join(root, f)
|
|
try:
|
|
content = open(path).read()
|
|
except Exception:
|
|
content = ""
|
|
results.append(
|
|
{
|
|
"title": f,
|
|
"description": path,
|
|
"url": path,
|
|
"content": content,
|
|
}
|
|
)
|
|
results.sort(key=lambda r: r["title"], reverse=True)
|
|
return results
|
|
|
|
|
|
def tool_cb(name: str, args: dict) -> str:
|
|
if name == "local_search":
|
|
return json.dumps(local_search(args.get("query", "")))
|
|
return ""
|
|
|
|
|
|
schema = json.dumps(
|
|
{
|
|
"type": "function",
|
|
"function": {
|
|
"name": "local_search",
|
|
"description": "Local filesystem search",
|
|
"parameters": {
|
|
"type": "object",
|
|
"properties": {"query": {"type": "string"}},
|
|
"required": ["query"],
|
|
},
|
|
},
|
|
}
|
|
)
|
|
|
|
runner = Runner(
|
|
which=Which.Plain(
|
|
model_id="NousResearch/Hermes-3-Llama-3.1-8B", arch=Architecture.Llama
|
|
),
|
|
tool_callbacks={"local_search": tool_cb},
|
|
)
|
|
|
|
res = runner.send_chat_completion_request(
|
|
ChatCompletionRequest(
|
|
model="default",
|
|
messages=[{"role": "user", "content": "Where is Cargo.toml in this repo?"}],
|
|
max_tokens=64,
|
|
tool_schemas=[schema],
|
|
tool_choice=ToolChoice.Auto,
|
|
)
|
|
)
|
|
print(res.choices[0].message.content)
|