-
Notifications
You must be signed in to change notification settings - Fork 1
/
llama_manager.py
76 lines (70 loc) · 3.06 KB
/
llama_manager.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
from transformers import pipeline
from openai import OpenAI
class LlamaManager:
def __init__(self, api_key):
self.client = OpenAI(api_key=api_key)
self.model_id = "meta-llama/Llama-3.2-3B-Instruct"
self.pipe = pipeline(
"text-generation",
model=self.model_id,
torch_dtype="bfloat16",
device_map="cuda:0",
max_new_tokens=800
)
def generate_response(self, conversation_history, system_prompt):
# Prepare user input with the last 20 messages from conversation history
user_prompt = "\n".join([f"{msg['role']}: {msg['content']}" for msg in conversation_history[-20:]])
# Generate a response from LLaMA
#outputs = self.pipe(
# prompt,
# max_new_tokens=800
#)
response = self.client.chat.completions.create(
model="gpt-4o-mini",
messages=[
{"role": "system", "content": system_prompt},
{"role": "user", "content": user_prompt}
],
max_tokens=800
)
generated_text = response.choices[0].message.content
return generated_text
def parse_ai_response(self, generated_text, participants):
# Simple parsing logic; can be improved with regex or more advanced NLP
lines = generated_text.strip().split('\n')
ai_responses = []
for line in lines:
if line.startswith("You:"):
continue # Skip user lines
if ':' in line:
name, content = line.split(':', 1)
name = name.strip()
content = content.strip()
# Parse action if present
if '(' in content and ')' in content:
message, action_part = content.split('(', 1)
message = message.strip()
action_part = action_part.strip(')')
if action_part.startswith("action:"):
action_details = action_part[len("action:"):].strip()
if action_details.startswith("interrupt"):
parts = action_details.split()
action = "interrupt"
time_wait = parts[1] if len(parts) > 1 else "0s"
ai_responses.append({
'name': name,
'content': message,
'action': action,
'time': time_wait
})
else:
ai_responses.append({
'name': name,
'content': message,
'action': action_details
})
else:
ai_responses.append({'name': name, 'content': content})
else:
ai_responses.append({'name': name, 'content': content})
return ai_responses