Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files- LLMAPI.py +21 -0
- LLMStore_v1.py +63 -0
LLMAPI.py
ADDED
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from openai import OpenAI
|
2 |
+
|
3 |
+
|
4 |
+
def get_LLM_response(user_input,systen_prompt):
|
5 |
+
client = OpenAI(
|
6 |
+
api_key = "sk-HWm8ZBWVs9DStt3MM1aVWTtelJndUJmoR7rdhaV72Sf9meZG", # key替换成你的API key
|
7 |
+
base_url = "https://api.moonshot.cn/v1",
|
8 |
+
)
|
9 |
+
|
10 |
+
completion = client.chat.completions.create(
|
11 |
+
model = "moonshot-v1-8k",
|
12 |
+
messages = [
|
13 |
+
{"role": "system", "content": systen_prompt},
|
14 |
+
{"role": "user", "content": user_input}
|
15 |
+
],
|
16 |
+
temperature = 0.3
|
17 |
+
)
|
18 |
+
|
19 |
+
return completion.choices[0].message.content
|
20 |
+
|
21 |
+
|
LLMStore_v1.py
ADDED
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from LLMAPI import get_LLM_response # 返回LLM的回复
|
3 |
+
|
4 |
+
# 使用会话状态来存储LLM名称列表, 思考这里为啥不用全局变量?
|
5 |
+
|
6 |
+
# 存储所有创建的LLM
|
7 |
+
llm_store = []
|
8 |
+
llm_list = []
|
9 |
+
|
10 |
+
# 创建LLM的功能
|
11 |
+
def create_llm(name, description, system_prompt):
|
12 |
+
llm = {
|
13 |
+
"name": name,
|
14 |
+
"description": description,
|
15 |
+
"system_prompt": system_prompt
|
16 |
+
}
|
17 |
+
llm_store.append(llm)
|
18 |
+
llm_list.append(name)
|
19 |
+
return f"LLM '{name}' 创建成功!", gr.Dropdown(choices=llm_list, interactive=True)
|
20 |
+
|
21 |
+
# 根据选择的LLM获取其详细信息
|
22 |
+
def get_llm_info(selected_llm_name):
|
23 |
+
for llm in llm_store:
|
24 |
+
if llm["name"] == selected_llm_name:
|
25 |
+
return llm["name"], llm["description"]
|
26 |
+
|
27 |
+
# 对话功能
|
28 |
+
def chat_with_llm(selected_llm_name, user_input):
|
29 |
+
for llm in llm_store:
|
30 |
+
if llm["name"] == selected_llm_name:
|
31 |
+
system_prompt = llm["system_prompt"]
|
32 |
+
# 这里可以调用你的LLM模型进行对话
|
33 |
+
response = get_LLM_response(user_input, system_prompt)
|
34 |
+
return response
|
35 |
+
|
36 |
+
# 创建Gradio界面
|
37 |
+
with gr.Blocks() as demo:
|
38 |
+
|
39 |
+
with gr.Tab("对话"):
|
40 |
+
gr.Markdown("## LLM对话")
|
41 |
+
llm_dropdown = gr.Dropdown(label="选择LLM", choices=[], interactive=True)
|
42 |
+
llm_name_display = gr.Textbox(label="LLM名称", interactive=False)
|
43 |
+
llm_description_display = gr.Textbox(label="LLM描述", interactive=False)
|
44 |
+
user_input = gr.Textbox(label="你的输入")
|
45 |
+
chat_button = gr.Button("发送")
|
46 |
+
chat_output = gr.Textbox(label="对话结果")
|
47 |
+
|
48 |
+
llm_dropdown.change(get_llm_info, inputs=llm_dropdown, outputs=[llm_name_display, llm_description_display])
|
49 |
+
chat_button.click(chat_with_llm, inputs=[llm_dropdown, user_input], outputs=chat_output)
|
50 |
+
|
51 |
+
|
52 |
+
with gr.Tab("创建LLM"):
|
53 |
+
gr.Markdown("## 创建一个自定义的LLM")
|
54 |
+
name_input = gr.Textbox(label="名称")
|
55 |
+
description_input = gr.Textbox(label="描述")
|
56 |
+
system_prompt_input = gr.Textbox(label="预制提示词/System Prompt")
|
57 |
+
create_button = gr.Button("创建")
|
58 |
+
create_output = gr.Textbox(label="创建结果")
|
59 |
+
|
60 |
+
create_button.click(create_llm, inputs=[name_input, description_input, system_prompt_input],
|
61 |
+
outputs=[create_output, llm_dropdown])
|
62 |
+
|
63 |
+
demo.launch(share=True) # share=True to make the app accessible to others
|