aifeifei798
commited on
Upload test_openai_api_lmstudio.py
Browse files- test_openai_api_lmstudio.py +32 -26
test_openai_api_lmstudio.py
CHANGED
@@ -1,3 +1,4 @@
|
|
|
|
1 |
from openai import OpenAI
|
2 |
|
3 |
# Point to the local server
|
@@ -9,29 +10,34 @@ history = [
|
|
9 |
|
10 |
# Open the file in read mode
|
11 |
with open('Model_Test_Issues_zh_en_jp.txt', 'r', encoding='utf-8') as file:
|
12 |
-
# Read
|
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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import random
|
2 |
from openai import OpenAI
|
3 |
|
4 |
# Point to the local server
|
|
|
10 |
|
11 |
# Open the file in read mode
|
12 |
with open('Model_Test_Issues_zh_en_jp.txt', 'r', encoding='utf-8') as file:
|
13 |
+
# Read all lines from the file
|
14 |
+
lines = file.readlines()
|
15 |
+
|
16 |
+
# Loop indefinitely
|
17 |
+
while True:
|
18 |
+
# Choose a random line from the file
|
19 |
+
line = random.choice(lines).strip()
|
20 |
+
print(line)
|
21 |
+
|
22 |
+
# Add the line as the user's content to the history
|
23 |
+
history.append({"role": "user", "content": line})
|
24 |
+
|
25 |
+
# Generate the response
|
26 |
+
completion = client.chat.completions.create(
|
27 |
+
model="mod/Repository",
|
28 |
+
messages=history,
|
29 |
+
temperature=0.7,
|
30 |
+
stream=True,
|
31 |
+
stop=["### Evaluation:","<|end_of_text|>","Translation:"]
|
32 |
+
)
|
33 |
+
|
34 |
+
new_message = {"role": "assistant", "content": ""}
|
35 |
+
|
36 |
+
for chunk in completion:
|
37 |
+
if chunk.choices[0].delta.content:
|
38 |
+
print(chunk.choices[0].delta.content, end="", flush=True)
|
39 |
+
new_message["content"] += chunk.choices[0].delta.content
|
40 |
+
|
41 |
+
history.append(new_message)
|
42 |
+
|
43 |
+
print()
|