Add Python scripts for Llama API chat clients, endpoint testing, and quick tests. Include documentation (README, CONTRIBUTING, 操作指南), license, and .gitignore. Supports multiple endpoints and models for OpenAI-compatible Llama API usage.
54 lines
1.4 KiB
Python
54 lines
1.4 KiB
Python
"""
|
|
快速測試內網 Llama API
|
|
"""
|
|
|
|
from openai import OpenAI
|
|
|
|
# API 設定
|
|
API_KEY = "paVrIT+XU1NhwCAOb0X4aYi75QKogK5YNMGvQF1dCyo="
|
|
BASE_URL = "http://192.168.0.6:21180/v1" # 使用第一個可用端點
|
|
|
|
def quick_test():
|
|
print("連接到內網 API...")
|
|
print(f"端點: {BASE_URL}")
|
|
print("-" * 50)
|
|
|
|
client = OpenAI(
|
|
api_key=API_KEY,
|
|
base_url=BASE_URL
|
|
)
|
|
|
|
# 測試對話
|
|
test_messages = [
|
|
"你好,請自我介紹",
|
|
"1 + 1 等於多少?",
|
|
"今天天氣如何?"
|
|
]
|
|
|
|
for msg in test_messages:
|
|
print(f"\n問: {msg}")
|
|
|
|
try:
|
|
response = client.chat.completions.create(
|
|
model="gpt-oss-120b",
|
|
messages=[
|
|
{"role": "user", "content": msg}
|
|
],
|
|
temperature=0.7,
|
|
max_tokens=200
|
|
)
|
|
|
|
answer = response.choices[0].message.content
|
|
# 清理可能的思考標記
|
|
if "<think>" in answer:
|
|
answer = answer.split("</think>")[-1].strip()
|
|
if "<|channel|>" in answer:
|
|
answer = answer.split("<|message|>")[-1].strip()
|
|
|
|
print(f"答: {answer}")
|
|
|
|
except Exception as e:
|
|
print(f"錯誤: {str(e)[:100]}")
|
|
|
|
if __name__ == "__main__":
|
|
quick_test() |