-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathorchestrate.py
More file actions
55 lines (43 loc) · 1.56 KB
/
Copy pathorchestrate.py
File metadata and controls
55 lines (43 loc) · 1.56 KB
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
import ollama
import os
from supabase import create_client
from retrieve import embed_query, match_chunks
from dotenv import load_dotenv
def build_prompt(usr_input, chunks):
chunks_text = '\n\n'.join(chunks) # 两个换行=空一行,更好区分
# prompots 之间故意想空一行让 prompt 更清晰( 但浪费token?)
prompt = f"""Your role: Helpful Assistant
Reference knowledge: {chunks_text}
User input: {usr_input}
How to respond: based on your knowledge plus the reference knowledge"""
return prompt
def generate_response(model, prompt):
# role: user 说的话,system 系统指令, assistant LLM 之前说的话,用于多轮对话
response = ollama.chat(model = model, messages = [{'role':'user', 'content':prompt}])
return response['message']['content']
# ollama 返回对象,大致长这样“
# {
# 'model': 'qwen3:4b',
# 'created_at': '2025-...',
# 'message': {
# 'role': 'assistant',
# 'content': '这里才是LLM真正的回复文本'
# },
# 'done': True,
# ...
# }
def main():
load_dotenv()
SUPABASE_URL = os.getenv('SUPABASE_URL')
SUPABASE_KEY = os.getenv('SUPABASE_KEY')
supabase = create_client(SUPABASE_URL, SUPABASE_KEY)
usr_query = 'test_query'
model = 'qwen3:4b'
usr_embed = embed_query(usr_query)
result = match_chunks(supabase, usr_embed)
chunks = [row['content'] for row in result.data]
prompt = build_prompt(usr_query, chunks)
reply = generate_response(model, prompt)
return reply
if __name__ == '__main__':
print(main())