-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathtest_lmstudio_noninteractive.py
More file actions
227 lines (193 loc) · 7.35 KB
/
Copy pathtest_lmstudio_noninteractive.py
File metadata and controls
227 lines (193 loc) · 7.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#!/usr/bin/env python3
"""
Non-interactive test version of lmstudio-tooluse-test.py
"""
import json
import shutil
from openai import OpenAI
# Initialize client (using proxy server)
client = OpenAI(base_url="http://127.0.0.1:5000", api_key="lm-studio")
MODEL = "glm-4.5-air-hi-mlx@4bit"
def fetch_wikipedia_content(search_query: str) -> dict:
"""Fetches wikipedia content for a given search_query"""
try:
import urllib.parse
import urllib.request
# Search for most relevant article
search_url = "https://en.wikipedia.org/w/api.php"
search_params = {
"action": "query",
"format": "json",
"list": "search",
"srsearch": search_query,
"srlimit": 1,
}
url = f"{search_url}?{urllib.parse.urlencode(search_params)}"
with urllib.request.urlopen(url) as response:
search_data = json.loads(response.read().decode())
if not search_data["query"]["search"]:
return {
"status": "error",
"message": f"No Wikipedia article found for '{search_query}'",
}
# Get the normalized title from search results
normalized_title = search_data["query"]["search"][0]["title"]
# Now fetch the actual content with the normalized title
content_params = {
"action": "query",
"format": "json",
"titles": normalized_title,
"prop": "extracts",
"exintro": "true",
"explaintext": "true",
"redirects": 1,
}
url = f"{search_url}?{urllib.parse.urlencode(content_params)}"
with urllib.request.urlopen(url) as response:
data = json.loads(response.read().decode())
pages = data["query"]["pages"]
page_id = list(pages.keys())[0]
if page_id == "-1":
return {
"status": "error",
"message": f"No Wikipedia article found for '{search_query}'",
}
content = pages[page_id]["extract"].strip()
return {
"status": "success",
"content": content,
"title": pages[page_id]["title"],
}
except Exception as e:
return {"status": "error", "message": str(e)}
# Define tool for LM Studio
WIKI_TOOL = {
"type": "function",
"function": {
"name": "fetch_wikipedia_content",
"description": (
"Search Wikipedia and fetch the introduction of the most relevant article. "
"Always use this if the user is asking for something that is likely on wikipedia. "
"If the user has a typo in their search query, correct it before searching."
),
"parameters": {
"type": "object",
"properties": {
"search_query": {
"type": "string",
"description": "Search query for finding the Wikipedia article",
},
},
"required": ["search_query"],
},
},
}
def test_query(user_input: str):
"""Test a single query"""
print(f"\n=== Testing Query: '{user_input}' ===")
messages = [
{
"role": "system",
"content": (
"You are an assistant that can retrieve Wikipedia articles. "
"When asked about a topic, you can retrieve Wikipedia articles "
"and cite information from them."
),
},
{"role": "user", "content": user_input}
]
try:
print("Sending request to proxy server...")
response = client.chat.completions.create(
model=MODEL,
messages=messages,
tools=[WIKI_TOOL],
)
print(f"Response received. Content: {response.choices[0].message.content}")
print(f"Tool calls: {bool(response.choices[0].message.tool_calls)}")
if response.choices[0].message.tool_calls:
print("✅ Tool calls detected!")
# Handle all tool calls
tool_calls = response.choices[0].message.tool_calls
# Add all tool calls to messages
messages.append(
{
"role": "assistant",
"tool_calls": [
{
"id": tool_call.id,
"type": tool_call.type,
"function": tool_call.function,
}
for tool_call in tool_calls
],
}
)
# Process each tool call and add results
for tool_call in tool_calls:
args = json.loads(tool_call.function.arguments)
result = fetch_wikipedia_content(args["search_query"])
# Print the Wikipedia content in a formatted way
terminal_width = min(shutil.get_terminal_size().columns, 80)
print("\n" + "=" * terminal_width)
if result["status"] == "success":
print(f"\nWikipedia article: {result['title']}")
print("-" * terminal_width)
# Truncate content for readability
content = result["content"]
if len(content) > 500:
content = content[:500] + "..."
print(content)
else:
print(f"\nError fetching Wikipedia content: {result['message']}")
print("=" * terminal_width)
messages.append(
{
"role": "tool",
"content": json.dumps(result),
"tool_call_id": tool_call.id,
}
)
# Get the post-tool-call response
print("\nGetting final response...")
final_response = client.chat.completions.create(
model=MODEL,
messages=messages
)
print(f"\nFinal Assistant Response:")
print(final_response.choices[0].message.content)
return True
else:
print("❌ No tool calls detected")
print(f"Regular response: {response.choices[0].message.content}")
return False
except Exception as e:
print(f"❌ Error: {e}")
return False
def main():
print("Testing LM Studio Tool Use with Proxy Server")
print("=" * 50)
# Test queries
test_queries = [
"tell me about lee jae myung",
"search for information about Python programming",
"what is machine learning?",
]
results = []
for query in test_queries:
success = test_query(query)
results.append((query, success))
print("\n" + "-" * 50)
print("\n" + "=" * 50)
print("Test Results Summary:")
for query, success in results:
status = "✅ SUCCESS" if success else "❌ FAILED"
print(f"{query}: {status}")
successful = sum(1 for _, success in results if success)
print(f"\nOverall: {successful}/{len(results)} tests passed")
if successful == len(results):
print("🎉 All tests passed! Tool call system is working correctly.")
else:
print("❌ Some tests failed. Check the implementation.")
if __name__ == "__main__":
main()