-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathagents.py
101 lines (83 loc) · 2.92 KB
/
agents.py
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
from swarm import Swarm, Agent
from openai import OpenAI
from run import run_demo_loop
import os
from minicpm import llama_cpp_inference
import subprocess
model = "deepseek-chat"
def transfer_to_agent_video():
return video_agent
def transfer_to_agent_audio():
return audio_agent
def transfer_back_to_triage():
"""Call this function if a user is asking about a topic that is not handled by the current agent."""
return triage_agent
def create_swarm():
client = OpenAI(
api_key=os.getenv("API_KEY"),
base_url=os.getenv("API_BASE")
)
# swarm = Swarm(client=client)
return Swarm(client=client)
def create_client():
client = OpenAI(
api_key=os.getenv("API_KEY"),
base_url=os.getenv("API_BASE")
)
return client
def init():
"""初始化"""
def asr(url):
"""语音识别"""
from funasr import AutoModel
# paraformer-zh is a multi-functional asr model
# use vad, punc, spk or not as you need
model = AutoModel(model="paraformer-zh", vad_model="fsmn-vad", punc_model="ct-punc")
res = model.generate(input=url,
batch_size_s=300,
hotword='')
return res
def file_process_by_ffmpeg(command):
try:
process = subprocess.run(command,shell=True, capture_output=True, text=True)
print(f"命令输出:\n{process.stdout}")
print(f"命令错误输出:\n{process.stderr}")
return f'处理完成'
except Exception as e:
return f"出现错误:" + str(e)
def video_question_answer(prompt, url):
"""视觉问答"""
return llama_cpp_inference(prompt,url)
triage_agent = Agent(
name="Triage Agent",
instructions="确定哪个代理最适合处理用户的请求,并将对话转交给该代理。",
functions=[transfer_to_agent_video, transfer_to_agent_audio],
model=model,
)
video_agent = Agent(
name="Video Agent",
instructions="负责处理视频或图像相关任务,并回复相关结果。会话完成或者出错返回triage_agent",
functions=[video_question_answer, file_process_by_ffmpeg, transfer_back_to_triage],
model=model,
)
audio_agent = Agent(
name="Audio Agent",
instructions="负责处理音频相关任务,并回复相关结果,会话完成或者出错返回triage_agent",
functions=[asr, file_process_by_ffmpeg, transfer_back_to_triage],
model=model,
)
def test():
client = create_client()
response = client.chat.completions.create(
model= model,
messages=[
{"role": "system", "content": "You are a helpful assistant"},
{"role": "user", "content": "请总结下这段视频的内容仅返回文本 /Users/whs/Downloads/section_6_011.55_013.96.mp4"},
],
stream=False
)
print(response)
print(response.choices[0].message.content)
if __name__ == "__main__":
# test()
run_demo_loop(triage_agent,client=create_client(), debug=True)