This commit is contained in:
55
llm/kimi.py
Normal file
55
llm/kimi.py
Normal file
@ -0,0 +1,55 @@
|
||||
from DrissionPage import Chromium, ChromiumOptions
|
||||
from DrissionPage.errors import ElementNotFoundError
|
||||
|
||||
|
||||
class Kimi:
|
||||
def __init__(self, base_timeout=30):
|
||||
self.base_timeout = base_timeout
|
||||
"""Initialize the Chromium browser with specified timeout settings."""
|
||||
# co = ChromiumOptions().auto_port()
|
||||
co = ChromiumOptions()
|
||||
co.set_timeouts(base=self.base_timeout)
|
||||
self.browser = Chromium(addr_or_opts=co)
|
||||
self.tab = self.browser.latest_tab
|
||||
self.tab.get('https://kimi.moonshot.cn/')
|
||||
self.first_time = True
|
||||
|
||||
def chat(self, input_message, wait_time=10):
|
||||
"""Send a message through the chat interface and retrieve the response."""
|
||||
try:
|
||||
chat_input = self.tab.ele('.editorContentEditable___FZJd9')
|
||||
chat_input.wait.enabled()
|
||||
chat_input.input(input_message)
|
||||
self.tab.wait(3)
|
||||
self.tab.ele('#send-button').wait.enabled()
|
||||
self.tab.ele('#send-button').click()
|
||||
self.tab.wait(wait_time)
|
||||
chat_input.input(' ')
|
||||
self.tab.ele('#send-button').wait.enabled()
|
||||
output = self.tab.ele('#^chat-markdown').text
|
||||
except ElementNotFoundError:
|
||||
output = ''
|
||||
return output
|
||||
|
||||
def generate(self, input_message, wait_time=10):
|
||||
if not self.first_time:
|
||||
new_session_btn = self.tab.ele('.myAgentTool___Y1_mC')
|
||||
new_session_btn.wait.enabled()
|
||||
new_session_btn.click()
|
||||
self.tab.wait(3)
|
||||
self.first_time = False
|
||||
return self.chat(input_message, wait_time)
|
||||
|
||||
def search(self, input_message, wait_time=10):
|
||||
return self.chat(input_message, wait_time)
|
||||
|
||||
def quit(self):
|
||||
self.browser.quit()
|
||||
|
||||
if __name__ == "__main__":
|
||||
kimi = Kimi()
|
||||
message = '今日新闻'
|
||||
response = kimi.search(message)
|
||||
kimi.quit()
|
||||
|
||||
print(response)
|
||||
56
llm/local/ollama.py
Normal file
56
llm/local/ollama.py
Normal file
@ -0,0 +1,56 @@
|
||||
import requests
|
||||
|
||||
|
||||
class Ollama:
|
||||
def __init__(self, model="qwen2.5:14b", server_url="http://192.168.1.200:11434"):
|
||||
self.model = model
|
||||
self.server_url = server_url
|
||||
|
||||
def is_service_running(self):
|
||||
"""检查 Ollama 服务是否正在运行"""
|
||||
try:
|
||||
url = f"{self.server_url}/api/generate"
|
||||
headers = {"Content-Type": "application/json"}
|
||||
payload = {
|
||||
"model": self.model,
|
||||
"prompt": "ping", # 使用轻量级的 prompt 测试
|
||||
"stream": False
|
||||
}
|
||||
output = requests.post(url, headers=headers, json=payload, timeout=60)
|
||||
if output.status_code == 200:
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
except requests.exceptions.RequestException:
|
||||
return False
|
||||
|
||||
def generate_text(self, input_message: str):
|
||||
url = f"{self.server_url}/api/generate"
|
||||
headers = {"Content-Type": "application/json"}
|
||||
payload = {
|
||||
"model": self.model,
|
||||
"prompt": input_message,
|
||||
"stream": False, # 关闭流式传输
|
||||
"options": {
|
||||
"temperature": 0.7,
|
||||
"top_p": 0.9,
|
||||
"num_ctx": 20480
|
||||
}
|
||||
}
|
||||
try:
|
||||
output = requests.post(url, headers=headers, json=payload)
|
||||
output.raise_for_status() # 检查HTTP请求是否成功
|
||||
result = output.json()
|
||||
return result.get("response", "No response from model.")
|
||||
except requests.exceptions.RequestException as e:
|
||||
return f"Error calling Ollama API: {e}"
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
# 示例提示文本
|
||||
ollama = Ollama()
|
||||
print("Ollama 服务是否正在运行:", ollama.is_service_running())
|
||||
prompt = "请用中文描述机器学习的基本概念。"
|
||||
response = ollama.generate_text(prompt)
|
||||
print("模型生成的文本:")
|
||||
print(response)
|
||||
41
llm/tongyi.py
Normal file
41
llm/tongyi.py
Normal file
@ -0,0 +1,41 @@
|
||||
from DrissionPage import Chromium, ChromiumOptions
|
||||
|
||||
class TongYi:
|
||||
def __init__(self, base_timeout=30):
|
||||
self.base_timeout = base_timeout
|
||||
"""Initialize the Chromium browser with specified timeout settings."""
|
||||
# co = ChromiumOptions().auto_port()
|
||||
co = ChromiumOptions()
|
||||
co.set_timeouts(base=self.base_timeout)
|
||||
self.browser = Chromium(addr_or_opts=co)
|
||||
self.tab = self.browser.latest_tab
|
||||
self.tab.get('https://tongyi.aliyun.com/')
|
||||
|
||||
def chat(self, input_message, wait_time=10):
|
||||
"""Send a message through the chat interface and retrieve the response."""
|
||||
chat_input = self.tab.ele('@placeholder=千事不决问通义')
|
||||
chat_input.input(input_message)
|
||||
self.tab.ele('.operateBtn--zFx6rSR0').click()
|
||||
self.tab.wait(wait_time)
|
||||
chat_input.input('over')
|
||||
self.tab.ele('.operateBtn--zFx6rSR0').wait.enabled()
|
||||
output = self.tab.ele('.containerWrap--lFLVsVCe').text
|
||||
return output
|
||||
|
||||
def generate(self, input_message, wait_time=10):
|
||||
return self.chat(input_message, wait_time)
|
||||
|
||||
def search(self, input_message, wait_time=10):
|
||||
return self.chat(input_message, wait_time)
|
||||
|
||||
def quit(self):
|
||||
self.browser.quit()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
tong_yi = TongYi()
|
||||
message = '今日新闻'
|
||||
response = tong_yi.search(message)
|
||||
tong_yi.quit()
|
||||
|
||||
print(response)
|
||||
Reference in New Issue
Block a user