phx-ashborn 0.1.3__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- phoenix/__init__.py +27 -0
- phoenix/agent/__init__.py +7 -0
- phoenix/agent/agent.py +81 -0
- phoenix/agent/loop.py +77 -0
- phoenix/audio/__init__.py +5 -0
- phoenix/audio/base.py +12 -0
- phoenix/audio/local.py +46 -0
- phoenix/audio/openai.py +63 -0
- phoenix/cache/__init__.py +4 -0
- phoenix/cache/base.py +12 -0
- phoenix/cache/redis_cache.py +37 -0
- phoenix/cognition/__init__.py +9 -0
- phoenix/cognition/analyzer.py +60 -0
- phoenix/cognition/planner.py +53 -0
- phoenix/cognition/reflector.py +39 -0
- phoenix/cognition/thinker.py +30 -0
- phoenix/core/__init__.py +1 -0
- phoenix/core/base.py +3 -0
- phoenix/core/config.py +69 -0
- phoenix/core/container.py +15 -0
- phoenix/core/exceptions.py +11 -0
- phoenix/core/hardware_check.py +110 -0
- phoenix/core/lifecycle.py +52 -0
- phoenix/core/utils.py +38 -0
- phoenix/db/__init__.py +4 -0
- phoenix/db/base.py +12 -0
- phoenix/db/sqlalchemy.py +20 -0
- phoenix/execution/__init__.py +7 -0
- phoenix/execution/actor.py +38 -0
- phoenix/execution/tool_manager.py +19 -0
- phoenix/framework/chatbot.py +226 -0
- phoenix/framework/security.py +101 -0
- phoenix/insight/__init__.py +13 -0
- phoenix/insight/base.py +7 -0
- phoenix/insight/composer.py +27 -0
- phoenix/insight/engine.py +120 -0
- phoenix/insight/optimizer.py +138 -0
- phoenix/insight/retriever.py +26 -0
- phoenix/insight/vlm_pipeline.py +142 -0
- phoenix/llm/__init__.py +7 -0
- phoenix/llm/base.py +22 -0
- phoenix/llm/local.py +179 -0
- phoenix/llm/openai.py +110 -0
- phoenix/llm/vlm_local.py +288 -0
- phoenix/llm/vlm_openai.py +110 -0
- phoenix/main.py +266 -0
- phoenix/memory/__init__.py +6 -0
- phoenix/memory/base.py +27 -0
- phoenix/memory/history.py +44 -0
- phoenix/memory/hybrid.py +72 -0
- phoenix/memory/long_term.py +38 -0
- phoenix/memory/manager.py +52 -0
- phoenix/memory/reflection.py +35 -0
- phoenix/memory/semantic.py +52 -0
- phoenix/memory/session.py +18 -0
- phoenix/memory/short_term.py +18 -0
- phoenix/observability/__init__.py +4 -0
- phoenix/observability/logger.py +30 -0
- phoenix/observability/tracing.py +46 -0
- phoenix/queue/__init__.py +4 -0
- phoenix/queue/base.py +6 -0
- phoenix/queue/celery_queue.py +16 -0
- phoenix/rag/__init__.py +3 -0
- phoenix/rag/pipeline.py +391 -0
- phoenix/tools/__init__.py +17 -0
- phoenix/tools/base.py +44 -0
- phoenix/tools/code.py +101 -0
- phoenix/tools/io.py +78 -0
- phoenix/tools/patch.py +58 -0
- phoenix/tools/registry.py +33 -0
- phoenix/tools/search.py +30 -0
- phoenix/training/base.py +29 -0
- phoenix/training/local_finetuner.py +141 -0
- phoenix/training/openai_finetuner.py +77 -0
- phoenix/vector/__init__.py +4 -0
- phoenix/vector/base.py +22 -0
- phoenix/vector/chroma.py +108 -0
- phoenix/vector/embeddings.py +52 -0
- phoenix/vector/qdrant.py +21 -0
- phx_ashborn-0.1.3.dist-info/METADATA +293 -0
- phx_ashborn-0.1.3.dist-info/RECORD +83 -0
- phx_ashborn-0.1.3.dist-info/WHEEL +5 -0
- phx_ashborn-0.1.3.dist-info/top_level.txt +1 -0
phoenix/__init__.py
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
from .main import (
|
|
2
|
+
init_phoenix,
|
|
3
|
+
startup_phoenix,
|
|
4
|
+
init_phoenix_full,
|
|
5
|
+
get_rag_pipeline,
|
|
6
|
+
set_providers,
|
|
7
|
+
get_providers,
|
|
8
|
+
get_insight_engine,
|
|
9
|
+
get_vlm_pipeline,
|
|
10
|
+
get_llm,
|
|
11
|
+
get_memory,
|
|
12
|
+
)
|
|
13
|
+
from .framework.chatbot import ChatBot
|
|
14
|
+
|
|
15
|
+
__all__ = [
|
|
16
|
+
"init_phoenix",
|
|
17
|
+
"startup_phoenix",
|
|
18
|
+
"init_phoenix_full",
|
|
19
|
+
"get_rag_pipeline",
|
|
20
|
+
"set_providers",
|
|
21
|
+
"get_providers",
|
|
22
|
+
"get_insight_engine",
|
|
23
|
+
"get_vlm_pipeline",
|
|
24
|
+
"get_llm",
|
|
25
|
+
"get_memory",
|
|
26
|
+
"ChatBot",
|
|
27
|
+
]
|
phoenix/agent/agent.py
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import uuid
|
|
2
|
+
from phoenix.llm.openai import OpenAILLM
|
|
3
|
+
from phoenix.memory.hybrid import HybridMemory
|
|
4
|
+
from phoenix.tools.registry import ToolRegistry
|
|
5
|
+
from phoenix.cognition.thinker import Thinker
|
|
6
|
+
from phoenix.cognition.planner import Planner
|
|
7
|
+
from phoenix.cognition.reflector import Reflector
|
|
8
|
+
from phoenix.cognition.analyzer import Analyzer
|
|
9
|
+
from phoenix.execution.actor import Actor
|
|
10
|
+
from phoenix.execution.tool_manager import ToolManager
|
|
11
|
+
from phoenix.agent.loop import AgentLoop
|
|
12
|
+
|
|
13
|
+
class Agent:
|
|
14
|
+
"""The main Agent class that integrates LLM, memory, tools, and cognition modules."""
|
|
15
|
+
def __init__(self, llm=None, memory=None, tools=None):
|
|
16
|
+
# Default LLM to OpenAILLM (using LongCat-Flash-Chat by default via its config fallback)
|
|
17
|
+
self.llm = llm if llm is not None else OpenAILLM()
|
|
18
|
+
self.memory = memory if memory is not None else HybridMemory()
|
|
19
|
+
self.tools = tools if tools is not None else ToolRegistry.load_default()
|
|
20
|
+
|
|
21
|
+
# Setup framework
|
|
22
|
+
self.thinker = Thinker(self.llm)
|
|
23
|
+
self.planner = Planner(self.llm, self.tools)
|
|
24
|
+
self.tool_manager = ToolManager(self.tools)
|
|
25
|
+
self.actor = Actor(self.tool_manager)
|
|
26
|
+
self.reflector = Reflector(self.llm)
|
|
27
|
+
self.analyzer = Analyzer(self.llm)
|
|
28
|
+
|
|
29
|
+
self.loop = AgentLoop(
|
|
30
|
+
thinker=self.thinker,
|
|
31
|
+
planner=self.planner,
|
|
32
|
+
actor=self.actor,
|
|
33
|
+
reflector=self.reflector,
|
|
34
|
+
analyzer=self.analyzer
|
|
35
|
+
)
|
|
36
|
+
|
|
37
|
+
def register_tool(self, tool):
|
|
38
|
+
"""Helper to register a new tool to the agent's ToolRegistry."""
|
|
39
|
+
self.tools.register(tool)
|
|
40
|
+
|
|
41
|
+
async def run(self, prompt: str, session_id: str = None, max_iterations: int = 15, mode: str = "auto") -> str:
|
|
42
|
+
"""
|
|
43
|
+
Run the agent with the given prompt.
|
|
44
|
+
mode can be "auto", "plan", or "fast_ans".
|
|
45
|
+
"""
|
|
46
|
+
# Auto-initialize LLM if it's our OpenAILLM and hasn't been initialized
|
|
47
|
+
if hasattr(self.llm, "client") and self.llm.client is None:
|
|
48
|
+
if hasattr(self.llm, "init"):
|
|
49
|
+
await self.llm.init()
|
|
50
|
+
|
|
51
|
+
if session_id is None:
|
|
52
|
+
session_id = str(uuid.uuid4())
|
|
53
|
+
|
|
54
|
+
# Add user prompt to memory
|
|
55
|
+
await self.memory.add_interaction(session_id, "user", prompt)
|
|
56
|
+
|
|
57
|
+
# Decide mode if auto
|
|
58
|
+
actual_mode = mode
|
|
59
|
+
if mode == "auto":
|
|
60
|
+
classification_prompt = (
|
|
61
|
+
f"Analyze the following user prompt and decide if it requires using tools and multi-step planning (like writing files, searching the web, or complex logic), "
|
|
62
|
+
f"or if it is a simple question that can be answered immediately.\n\n"
|
|
63
|
+
f"Prompt: {prompt}\n\n"
|
|
64
|
+
f"Respond with exactly one word: 'PLAN' or 'FAST'."
|
|
65
|
+
)
|
|
66
|
+
classification = await self.llm.generate(classification_prompt, session_id=None)
|
|
67
|
+
if "PLAN" in classification.upper():
|
|
68
|
+
actual_mode = "plan"
|
|
69
|
+
else:
|
|
70
|
+
actual_mode = "fast_ans"
|
|
71
|
+
|
|
72
|
+
if actual_mode == "fast_ans":
|
|
73
|
+
# Fast answer mode: bypass the agent loop and just use the LLM directly with memory context
|
|
74
|
+
context = await self.memory.get_full_context(session_id, query=prompt)
|
|
75
|
+
fast_prompt = f"Context:\n{context}\n\nUser: {prompt}\nAnswer directly:"
|
|
76
|
+
fast_answer = await self.llm.generate(fast_prompt, session_id=None)
|
|
77
|
+
await self.memory.add_interaction(session_id, "assistant", fast_answer)
|
|
78
|
+
return fast_answer
|
|
79
|
+
else:
|
|
80
|
+
# Start execution loop
|
|
81
|
+
return await self.loop.run(prompt, self.memory, session_id, max_iterations=max_iterations)
|
phoenix/agent/loop.py
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
from phoenix.cognition.thinker import Thinker
|
|
2
|
+
from phoenix.cognition.planner import Planner
|
|
3
|
+
from phoenix.cognition.reflector import Reflector
|
|
4
|
+
from phoenix.cognition.analyzer import Analyzer
|
|
5
|
+
from phoenix.execution.actor import Actor
|
|
6
|
+
import asyncio
|
|
7
|
+
|
|
8
|
+
class AgentLoop:
|
|
9
|
+
"""Coordinates the Think -> Plan -> Act -> Reflect cycle."""
|
|
10
|
+
def __init__(self, thinker: Thinker, planner: Planner, actor: Actor, reflector: Reflector, analyzer: Analyzer):
|
|
11
|
+
self.thinker = thinker
|
|
12
|
+
self.planner = planner
|
|
13
|
+
self.actor = actor
|
|
14
|
+
self.reflector = reflector
|
|
15
|
+
self.analyzer = analyzer
|
|
16
|
+
|
|
17
|
+
async def run(self, prompt: str, memory, session_id: str, max_iterations: int = 5) -> str:
|
|
18
|
+
# Step 1 & 2: Parallel Awareness (Think + Analyze)
|
|
19
|
+
think_task = self.thinker.analyze(prompt, memory, session_id)
|
|
20
|
+
analyze_task = self.analyzer.analyze_workspace(prompt)
|
|
21
|
+
|
|
22
|
+
# Concurrent execution of the initial cognitive steps
|
|
23
|
+
objective, analysis = await asyncio.gather(think_task, analyze_task)
|
|
24
|
+
|
|
25
|
+
# Store results in session state
|
|
26
|
+
memory.session.set("current_objective", objective)
|
|
27
|
+
memory.session.set("project_analysis", analysis)
|
|
28
|
+
|
|
29
|
+
# Concurrent system logging
|
|
30
|
+
await asyncio.gather(
|
|
31
|
+
memory.add_interaction(session_id, "system", f"Identified Objective: {objective}"),
|
|
32
|
+
memory.add_interaction(session_id, "system", f"Project Analysis: {analysis['tech_stack']}")
|
|
33
|
+
)
|
|
34
|
+
|
|
35
|
+
previous_results = ""
|
|
36
|
+
final_answer = ""
|
|
37
|
+
|
|
38
|
+
for i in range(max_iterations):
|
|
39
|
+
# Step 3: Plan (select actions)
|
|
40
|
+
plan = await self.planner.plan(objective, previous_results)
|
|
41
|
+
|
|
42
|
+
# Step 4: Act (execute tools in parallel)
|
|
43
|
+
actions = plan.get("actions", [])
|
|
44
|
+
if not actions and "tool" in plan:
|
|
45
|
+
actions = [{"tool": plan["tool"]}]
|
|
46
|
+
|
|
47
|
+
if any(a.get("tool") == "finish" for a in actions):
|
|
48
|
+
final_answer = previous_results or "Task completed without actions."
|
|
49
|
+
break
|
|
50
|
+
|
|
51
|
+
action_result = await self.actor.execute(plan)
|
|
52
|
+
|
|
53
|
+
# Step 5: Reflect (evaluate progress)
|
|
54
|
+
reflection = await self.reflector.reflect(objective, plan, action_result)
|
|
55
|
+
|
|
56
|
+
# Parallel update of memory and reflection state
|
|
57
|
+
memory.reflection.add_reflection(reflection["reflection"])
|
|
58
|
+
await asyncio.gather(
|
|
59
|
+
memory.consolidate_reflections(self.reflector.llm),
|
|
60
|
+
memory.add_interaction(
|
|
61
|
+
session_id,
|
|
62
|
+
"system",
|
|
63
|
+
f"Action: {plan}\nResult: {action_result}\nReflection: {reflection['reflection']}"
|
|
64
|
+
)
|
|
65
|
+
)
|
|
66
|
+
|
|
67
|
+
previous_results += f"\nAction: {plan}\nResult: {action_result}\n"
|
|
68
|
+
|
|
69
|
+
if reflection["is_complete"]:
|
|
70
|
+
final_answer = action_result
|
|
71
|
+
break
|
|
72
|
+
|
|
73
|
+
if not final_answer:
|
|
74
|
+
final_answer = "Agent stopped after reaching maximum iterations without completing the objective."
|
|
75
|
+
|
|
76
|
+
await memory.add_interaction(session_id, "assistant", final_answer)
|
|
77
|
+
return final_answer
|
phoenix/audio/base.py
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
from phoenix.core.base import BaseService
|
|
2
|
+
|
|
3
|
+
class BaseSTT(BaseService):
|
|
4
|
+
async def transcribe(self, audio_path: str, lang: str = "en-US") -> str:
|
|
5
|
+
"""Transcribe speech to text from an audio file."""
|
|
6
|
+
raise NotImplementedError
|
|
7
|
+
|
|
8
|
+
class BaseTTS(BaseService):
|
|
9
|
+
async def synthesize(self, text: str, output_path: str, lang: str = "en") -> str:
|
|
10
|
+
"""Synthesize text to speech and save to output_path."""
|
|
11
|
+
raise NotImplementedError
|
|
12
|
+
|
phoenix/audio/local.py
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
from phoenix.audio.base import BaseSTT, BaseTTS
|
|
2
|
+
import asyncio
|
|
3
|
+
import os
|
|
4
|
+
|
|
5
|
+
class LocalSTT(BaseSTT):
|
|
6
|
+
def __init__(self):
|
|
7
|
+
self.recognizer = None
|
|
8
|
+
|
|
9
|
+
async def init(self):
|
|
10
|
+
import speech_recognition as sr
|
|
11
|
+
self.recognizer = sr.Recognizer()
|
|
12
|
+
|
|
13
|
+
async def transcribe(self, audio_path: str, lang: str = "en-US") -> str:
|
|
14
|
+
if not self.recognizer:
|
|
15
|
+
await self.init()
|
|
16
|
+
|
|
17
|
+
import speech_recognition as sr
|
|
18
|
+
try:
|
|
19
|
+
with sr.AudioFile(audio_path) as source:
|
|
20
|
+
audio_data = self.recognizer.record(source)
|
|
21
|
+
# Using Google's free web search API for transcription
|
|
22
|
+
text = self.recognizer.recognize_google(audio_data, language=lang)
|
|
23
|
+
return text
|
|
24
|
+
except Exception as e:
|
|
25
|
+
return f"Error transcribing audio: {str(e)}"
|
|
26
|
+
|
|
27
|
+
class LocalTTS(BaseTTS):
|
|
28
|
+
def __init__(self):
|
|
29
|
+
self.initialized = False
|
|
30
|
+
|
|
31
|
+
async def init(self):
|
|
32
|
+
self.initialized = True
|
|
33
|
+
|
|
34
|
+
async def synthesize(self, text: str, output_path: str, lang: str = "en") -> str:
|
|
35
|
+
from gtts import gTTS
|
|
36
|
+
try:
|
|
37
|
+
# Generate speech using gTTS (Google Text-to-Speech)
|
|
38
|
+
tts = gTTS(text=text, lang=lang)
|
|
39
|
+
# gTTS save is blocking, so run in executor if needed,
|
|
40
|
+
# but for simplicity in SDK we'll just call it.
|
|
41
|
+
tts.save(output_path)
|
|
42
|
+
return output_path
|
|
43
|
+
|
|
44
|
+
except Exception as e:
|
|
45
|
+
raise RuntimeError(f"TTS Synthesis failed: {e}")
|
|
46
|
+
|
phoenix/audio/openai.py
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import os
|
|
2
|
+
from phoenix.audio.base import BaseSTT, BaseTTS
|
|
3
|
+
from phoenix.core.config import config
|
|
4
|
+
from openai import OpenAI
|
|
5
|
+
|
|
6
|
+
class OpenAISTT(BaseSTT):
|
|
7
|
+
def __init__(self):
|
|
8
|
+
self.api_key = config.OPENAI_API_KEY
|
|
9
|
+
self.base_url = config.OPENAI_BASE_URL
|
|
10
|
+
self.client = None
|
|
11
|
+
|
|
12
|
+
async def init(self):
|
|
13
|
+
if not self.api_key:
|
|
14
|
+
print("Warning: OPENAI_API_KEY is missing. Operating in mock mode.")
|
|
15
|
+
self.client = OpenAI(
|
|
16
|
+
api_key=self.api_key,
|
|
17
|
+
base_url=self.base_url,
|
|
18
|
+
)
|
|
19
|
+
|
|
20
|
+
async def transcribe(self, audio_path: str, model: str = "whisper-1") -> str:
|
|
21
|
+
if not self.client:
|
|
22
|
+
raise RuntimeError("OpenAISTT is not initialized.")
|
|
23
|
+
if not self.api_key:
|
|
24
|
+
return f"[Mock OpenAISTT Transcription (No API Key) for: {audio_path}]"
|
|
25
|
+
|
|
26
|
+
with open(audio_path, "rb") as audio_file:
|
|
27
|
+
transcript = self.client.audio.transcriptions.create(
|
|
28
|
+
model=model,
|
|
29
|
+
file=audio_file
|
|
30
|
+
)
|
|
31
|
+
return transcript.text
|
|
32
|
+
|
|
33
|
+
class OpenAITTS(BaseTTS):
|
|
34
|
+
def __init__(self):
|
|
35
|
+
self.api_key = config.OPENAI_API_KEY
|
|
36
|
+
self.base_url = config.OPENAI_BASE_URL
|
|
37
|
+
self.client = None
|
|
38
|
+
|
|
39
|
+
async def init(self):
|
|
40
|
+
if not self.api_key:
|
|
41
|
+
print("Warning: OPENAI_API_KEY is missing. Operating in mock mode.")
|
|
42
|
+
self.client = OpenAI(
|
|
43
|
+
api_key=self.api_key,
|
|
44
|
+
base_url=self.base_url,
|
|
45
|
+
)
|
|
46
|
+
|
|
47
|
+
async def synthesize(self, text: str, output_path: str, model: str = "tts-1", voice: str = "alloy") -> str:
|
|
48
|
+
if not self.client:
|
|
49
|
+
raise RuntimeError("OpenAITTS is not initialized.")
|
|
50
|
+
if not self.api_key:
|
|
51
|
+
with open(output_path, "wb") as f:
|
|
52
|
+
f.write(b"Mock OpenAI audio data")
|
|
53
|
+
return output_path
|
|
54
|
+
|
|
55
|
+
response = self.client.audio.speech.create(
|
|
56
|
+
model=model,
|
|
57
|
+
voice=voice,
|
|
58
|
+
input=text
|
|
59
|
+
)
|
|
60
|
+
with open(output_path, "wb") as f:
|
|
61
|
+
for chunk in response.iter_bytes():
|
|
62
|
+
f.write(chunk)
|
|
63
|
+
return output_path
|
phoenix/cache/base.py
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
from typing import Any, Optional
|
|
2
|
+
from phoenix.core.base import BaseService
|
|
3
|
+
|
|
4
|
+
class BaseCache(BaseService):
|
|
5
|
+
async def get(self, key: str) -> Optional[Any]:
|
|
6
|
+
raise NotImplementedError
|
|
7
|
+
|
|
8
|
+
async def set(self, key: str, value: Any, ttl: int) -> None:
|
|
9
|
+
raise NotImplementedError
|
|
10
|
+
|
|
11
|
+
async def delete(self, key: str) -> None:
|
|
12
|
+
raise NotImplementedError
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import json
|
|
2
|
+
import redis.asyncio as redis
|
|
3
|
+
from typing import Any, Optional
|
|
4
|
+
from phoenix.cache.base import BaseCache
|
|
5
|
+
from phoenix.core.config import config
|
|
6
|
+
|
|
7
|
+
class RedisCache(BaseCache):
|
|
8
|
+
def __init__(self):
|
|
9
|
+
self.redis = None
|
|
10
|
+
|
|
11
|
+
async def init(self):
|
|
12
|
+
self.redis = redis.from_url(config.REDIS_URL, decode_responses=True)
|
|
13
|
+
|
|
14
|
+
async def get(self, key: str) -> Optional[Any]:
|
|
15
|
+
if not self.redis:
|
|
16
|
+
await self.init()
|
|
17
|
+
value = await self.redis.get(key)
|
|
18
|
+
if value:
|
|
19
|
+
try:
|
|
20
|
+
return json.loads(value)
|
|
21
|
+
except json.JSONDecodeError:
|
|
22
|
+
return value
|
|
23
|
+
return None
|
|
24
|
+
|
|
25
|
+
async def set(self, key: str, value: Any, ttl: int) -> None:
|
|
26
|
+
if not self.redis:
|
|
27
|
+
await self.init()
|
|
28
|
+
try:
|
|
29
|
+
serialized_value = json.dumps(value)
|
|
30
|
+
except TypeError:
|
|
31
|
+
serialized_value = str(value)
|
|
32
|
+
await self.redis.set(key, serialized_value, ex=ttl)
|
|
33
|
+
|
|
34
|
+
async def delete(self, key: str) -> None:
|
|
35
|
+
if not self.redis:
|
|
36
|
+
await self.init()
|
|
37
|
+
await self.redis.delete(key)
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import os
|
|
2
|
+
from typing import List, Dict
|
|
3
|
+
from phoenix.llm.base import BaseLLM
|
|
4
|
+
|
|
5
|
+
class Analyzer:
|
|
6
|
+
"""
|
|
7
|
+
Scans the repository and identifies relevant files and architectural patterns
|
|
8
|
+
based on the user's prompt to assist the Planner.
|
|
9
|
+
"""
|
|
10
|
+
def __init__(self, llm: BaseLLM):
|
|
11
|
+
self.llm = llm
|
|
12
|
+
|
|
13
|
+
async def analyze_workspace(self, prompt: str, root_dir: str = ".") -> Dict:
|
|
14
|
+
"""
|
|
15
|
+
Analyzes the root directory and identifies relevant files for the prompt.
|
|
16
|
+
"""
|
|
17
|
+
# Get a list of files in the workspace (shallow)
|
|
18
|
+
files = []
|
|
19
|
+
for root, dirs, filenames in os.walk(root_dir):
|
|
20
|
+
# Ignore common noise directories
|
|
21
|
+
if any(p in root for p in [".git", "__pycache__", "venv", ".env", "node_modules"]):
|
|
22
|
+
continue
|
|
23
|
+
for f in filenames:
|
|
24
|
+
files.append(os.path.join(root, f))
|
|
25
|
+
if len(files) > 100: # Limit for speed
|
|
26
|
+
break
|
|
27
|
+
if len(files) > 100:
|
|
28
|
+
break
|
|
29
|
+
|
|
30
|
+
files_str = "\n".join(files)
|
|
31
|
+
|
|
32
|
+
system_prompt = """
|
|
33
|
+
You are the 'Analyzer' module of an autonomous agent.
|
|
34
|
+
Your job is to look at the list of files in the project and identify which ones are most relevant
|
|
35
|
+
to the user's request. Also, try to identify the tech stack or core architecture.
|
|
36
|
+
|
|
37
|
+
Respond with exactly this JSON format:
|
|
38
|
+
{
|
|
39
|
+
"relevant_files": ["file1", "file2"],
|
|
40
|
+
"tech_stack": "e.g. Python/FastAPI",
|
|
41
|
+
"summary": "Brief architectural summary"
|
|
42
|
+
}
|
|
43
|
+
"""
|
|
44
|
+
|
|
45
|
+
full_prompt = f"{system_prompt}\n\nFiles in workspace:\n{files_str}\n\nUser Prompt: {prompt}\n\nAnalysis (JSON):"
|
|
46
|
+
response = await self.llm.generate(full_prompt, session_id=None)
|
|
47
|
+
|
|
48
|
+
import json
|
|
49
|
+
import re
|
|
50
|
+
try:
|
|
51
|
+
match = re.search(r'```(?:json)?(.*?)```', response, re.DOTALL)
|
|
52
|
+
if match:
|
|
53
|
+
response = match.group(1)
|
|
54
|
+
return json.loads(response.strip())
|
|
55
|
+
except:
|
|
56
|
+
return {
|
|
57
|
+
"relevant_files": [],
|
|
58
|
+
"tech_stack": "Unknown",
|
|
59
|
+
"summary": "Failed to analyze workspace"
|
|
60
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
from phoenix.llm.base import BaseLLM
|
|
2
|
+
from phoenix.tools.registry import ToolRegistry
|
|
3
|
+
import json
|
|
4
|
+
import re
|
|
5
|
+
|
|
6
|
+
class Planner:
|
|
7
|
+
"""Generates actionable steps and selects tools based on the Thinker's objective."""
|
|
8
|
+
def __init__(self, llm: BaseLLM, tools: ToolRegistry):
|
|
9
|
+
self.llm = llm
|
|
10
|
+
self.tools = tools
|
|
11
|
+
self._cached_tool_info = None
|
|
12
|
+
|
|
13
|
+
async def plan(self, objective: str, previous_results: str = "") -> dict:
|
|
14
|
+
if self._cached_tool_info is None:
|
|
15
|
+
self._cached_tool_info = json.dumps(self.tools.get_all_tools_info(), indent=2)
|
|
16
|
+
|
|
17
|
+
available_tools = self._cached_tool_info
|
|
18
|
+
|
|
19
|
+
system_prompt = f"""
|
|
20
|
+
You are the 'Planner' module of an autonomous agent.
|
|
21
|
+
Based on the given objective, formulate the next action using one of the available tools.
|
|
22
|
+
If the objective is already met based on previous results, you can use a 'finish' tool.
|
|
23
|
+
|
|
24
|
+
Available Tools:
|
|
25
|
+
{available_tools}
|
|
26
|
+
|
|
27
|
+
Previous Results:
|
|
28
|
+
{previous_results}
|
|
29
|
+
|
|
30
|
+
You must respond with a JSON object strictly following this format:
|
|
31
|
+
{{
|
|
32
|
+
"actions": [
|
|
33
|
+
{{"tool": "tool_name", "kwargs": {{"arg1": "value1"}}}},
|
|
34
|
+
{{"tool": "tool_name", "kwargs": {{"arg1": "value1"}}}}
|
|
35
|
+
]
|
|
36
|
+
}}
|
|
37
|
+
If you believe the task is complete, use "tool": "finish".
|
|
38
|
+
You can specify multiple independent actions if they can be performed simultaneously.
|
|
39
|
+
"""
|
|
40
|
+
|
|
41
|
+
full_prompt = f"{system_prompt}\n\nObjective: {objective}\n\nPlan (JSON only):"
|
|
42
|
+
response = await self.llm.generate(full_prompt, session_id=None)
|
|
43
|
+
|
|
44
|
+
# Clean up JSON if LLM added markdown formatting
|
|
45
|
+
try:
|
|
46
|
+
# Extract json blocks if any
|
|
47
|
+
match = re.search(r'```(?:json)?(.*?)```', response, re.DOTALL)
|
|
48
|
+
if match:
|
|
49
|
+
response = match.group(1)
|
|
50
|
+
return json.loads(response.strip())
|
|
51
|
+
except json.JSONDecodeError:
|
|
52
|
+
# Fallback for bad JSON
|
|
53
|
+
return {"tool": "finish", "kwargs": {"reason": "Failed to parse planner output"}}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
from phoenix.llm.base import BaseLLM
|
|
2
|
+
|
|
3
|
+
class Reflector:
|
|
4
|
+
"""Evaluates the outcome of actions and determines task completion or replanning."""
|
|
5
|
+
def __init__(self, llm: BaseLLM):
|
|
6
|
+
self.llm = llm
|
|
7
|
+
|
|
8
|
+
async def reflect(self, objective: str, action: dict, result: str) -> dict:
|
|
9
|
+
system_prompt = """
|
|
10
|
+
You are the 'Reflector' module of an autonomous agent.
|
|
11
|
+
Your job is to evaluate the result of the last action taken towards achieving the objective.
|
|
12
|
+
|
|
13
|
+
Determine:
|
|
14
|
+
1. Is the objective fully accomplished?
|
|
15
|
+
2. What should be learned or saved from this action?
|
|
16
|
+
|
|
17
|
+
Respond strictly with JSON:
|
|
18
|
+
{
|
|
19
|
+
"is_complete": boolean,
|
|
20
|
+
"reflection": "A short summary of what was learned or what needs to be done next."
|
|
21
|
+
}
|
|
22
|
+
"""
|
|
23
|
+
|
|
24
|
+
full_prompt = f"{system_prompt}\n\nObjective: {objective}\nAction Taken: {action}\nResult: {result}\n\nReflection (JSON only):"
|
|
25
|
+
response = await self.llm.generate(full_prompt, session_id=None)
|
|
26
|
+
|
|
27
|
+
import json
|
|
28
|
+
import re
|
|
29
|
+
try:
|
|
30
|
+
match = re.search(r'```(?:json)?(.*?)```', response, re.DOTALL)
|
|
31
|
+
if match:
|
|
32
|
+
response = match.group(1)
|
|
33
|
+
data = json.loads(response.strip())
|
|
34
|
+
return {
|
|
35
|
+
"is_complete": bool(data.get("is_complete", False)),
|
|
36
|
+
"reflection": str(data.get("reflection", ""))
|
|
37
|
+
}
|
|
38
|
+
except json.JSONDecodeError:
|
|
39
|
+
return {"is_complete": False, "reflection": "Failed to parse reflection"}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
from phoenix.llm.base import BaseLLM
|
|
2
|
+
from phoenix.memory.hybrid import HybridMemory
|
|
3
|
+
|
|
4
|
+
class Thinker:
|
|
5
|
+
"""Analyzes user prompts, breaks them down, and understands core objectives."""
|
|
6
|
+
def __init__(self, llm: BaseLLM):
|
|
7
|
+
self.llm = llm
|
|
8
|
+
|
|
9
|
+
async def analyze(self, prompt: str, memory: HybridMemory, session_id: str) -> str:
|
|
10
|
+
context = await memory.get_full_context(session_id, query=prompt)
|
|
11
|
+
|
|
12
|
+
system_prompt = f"""
|
|
13
|
+
You are the 'Thinker' module, the lead architect of an autonomous agent.
|
|
14
|
+
Your job is to deconstruct complex user prompts into a refined, actionable objective.
|
|
15
|
+
|
|
16
|
+
Guidelines:
|
|
17
|
+
1. Identify the 'Core Intent' - what does the user actually want?
|
|
18
|
+
2. Identify 'Implicit Requirements' - what else needs to happen for this to be correct?
|
|
19
|
+
3. Define 'Success Criteria' - how will we know the task is done?
|
|
20
|
+
|
|
21
|
+
Context from Memory:
|
|
22
|
+
{context}
|
|
23
|
+
"""
|
|
24
|
+
|
|
25
|
+
full_prompt = (
|
|
26
|
+
f"{system_prompt}\n\n"
|
|
27
|
+
f"User Request: {prompt}\n\n"
|
|
28
|
+
"Respond with a comprehensive Objective Analysis (Core Intent + Requirements + Success Criteria):"
|
|
29
|
+
)
|
|
30
|
+
return await self.llm.generate(full_prompt, session_id=None)
|
phoenix/core/__init__.py
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"""Core module for phoenix."""
|
phoenix/core/base.py
ADDED
phoenix/core/config.py
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import os
|
|
2
|
+
from dotenv import load_dotenv
|
|
3
|
+
|
|
4
|
+
# Load .env file if present
|
|
5
|
+
load_dotenv(override=True)
|
|
6
|
+
|
|
7
|
+
class Config:
|
|
8
|
+
REDIS_URL = os.getenv("REDIS_URL", "redis://localhost:6379")
|
|
9
|
+
|
|
10
|
+
# LLM Config
|
|
11
|
+
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY", "")
|
|
12
|
+
OPENAI_BASE_URL = os.getenv("OPENAI_BASE_URL", "https://api.openai.com/v1")
|
|
13
|
+
OPENAI_LLM_MODEL = os.getenv("OPENAI_LLM_MODEL", "gpt-4o")
|
|
14
|
+
|
|
15
|
+
# VLM Config
|
|
16
|
+
OPENAI_VLM_API_KEY = os.getenv("OPENAI_VLM_API_KEY", "")
|
|
17
|
+
OPENAI_VLM_BASE_URL = os.getenv("OPENAI_VLM_BASE_URL", "")
|
|
18
|
+
OPENAI_VLM_MODEL = os.getenv("OPENAI_VLM_MODEL", "")
|
|
19
|
+
QDRANT_URL = os.getenv("QDRANT_URL", "http://localhost:6333")
|
|
20
|
+
|
|
21
|
+
# Vector DB Config
|
|
22
|
+
VECTOR_DB_TYPE = os.getenv("VECTOR_DB_TYPE", "chroma")
|
|
23
|
+
CHROMA_PERSIST_DIR = os.getenv("CHROMA_PERSIST_DIR", "./chroma_db")
|
|
24
|
+
|
|
25
|
+
# Embedding Config
|
|
26
|
+
EMBEDDING_MODEL = os.getenv("EMBEDDING_MODEL", "all-MiniLM-L6-v2")
|
|
27
|
+
|
|
28
|
+
# Local LLM's Config
|
|
29
|
+
# Qwen/Qwen2-1.5B-Instruct
|
|
30
|
+
LOCAL_LLM_TEXT_MODEL = os.getenv("LOCAL_LLM_TEXT_MODEL", "")
|
|
31
|
+
LOCAL_LLM_EMBED_MODEL = os.getenv("LOCAL_LLM_EMBED_MODEL", "all-MiniLM-L6-v2")
|
|
32
|
+
# Qwen/Qwen2-VL-2B-Instruct
|
|
33
|
+
LOCAL_VLM_MODEL = os.getenv("LOCAL_VLM_MODEL", "")
|
|
34
|
+
|
|
35
|
+
# Deferred Loading Flags
|
|
36
|
+
LOAD_LOCAL_LLM = os.getenv("LOAD_LOCAL_LLM", "false").lower() == "true"
|
|
37
|
+
LOAD_LOCAL_VLM = os.getenv("LOAD_LOCAL_VLM", "false").lower() == "true"
|
|
38
|
+
|
|
39
|
+
# Fallback Behavior
|
|
40
|
+
AUTO_ACCEPT_FALLBACK = os.getenv("AUTO_ACCEPT_FALLBACK", "false").lower() == "true"
|
|
41
|
+
|
|
42
|
+
# Training Config
|
|
43
|
+
FINETUNE_PROVIDER = os.getenv("FINETUNE_PROVIDER", "local") # "local" or "openai"
|
|
44
|
+
TRAINING_OUTPUT_DIR = os.getenv("TRAINING_OUTPUT_DIR", "./finetuned_models")
|
|
45
|
+
TRAINING_BATCH_SIZE = int(os.getenv("TRAINING_BATCH_SIZE", "1"))
|
|
46
|
+
TRAINING_EPOCHS = int(os.getenv("TRAINING_EPOCHS", "1"))
|
|
47
|
+
TRAINING_LEARNING_RATE = float(os.getenv("TRAINING_LEARNING_RATE", "2e-4"))
|
|
48
|
+
TRAINING_LORA_R = int(os.getenv("TRAINING_LORA_R", "8"))
|
|
49
|
+
|
|
50
|
+
# Security & Token Config
|
|
51
|
+
SECURITY_MAX_INPUT_LENGTH = int(os.getenv("SECURITY_MAX_INPUT_LENGTH", "4096"))
|
|
52
|
+
SECURITY_MAX_OUTPUT_LENGTH = int(os.getenv("SECURITY_MAX_OUTPUT_LENGTH", "2048"))
|
|
53
|
+
SECURITY_ENABLE_HALLUCINATION_CHECK = os.getenv("SECURITY_ENABLE_HALLUCINATION_CHECK", "false").lower() == "true"
|
|
54
|
+
|
|
55
|
+
# RAG Optimization Config
|
|
56
|
+
RAG_QUERY_EXPANSION = os.getenv("RAG_QUERY_EXPANSION", "true").lower() == "true"
|
|
57
|
+
RAG_HYDE_ENABLED = os.getenv("RAG_HYDE_ENABLED", "true").lower() == "true"
|
|
58
|
+
RAG_MMR_ENABLED = os.getenv("RAG_MMR_ENABLED", "true").lower() == "true"
|
|
59
|
+
RAG_MMR_LAMBDA = float(os.getenv("RAG_MMR_LAMBDA", "0.5"))
|
|
60
|
+
RAG_MAX_CONTEXT_CHUNKS = int(os.getenv("RAG_MAX_CONTEXT_CHUNKS", "5"))
|
|
61
|
+
RAG_CONTEXT_COMPRESSION = os.getenv("RAG_CONTEXT_COMPRESSION", "true").lower() == "true"
|
|
62
|
+
|
|
63
|
+
# Parent-Child Retrieval Config
|
|
64
|
+
RAG_PARENT_RETRIEVAL = os.getenv("RAG_PARENT_RETRIEVAL", "true").lower() == "true"
|
|
65
|
+
RAG_PARENT_CHUNK_SIZE = int(os.getenv("RAG_PARENT_CHUNK_SIZE", "1500"))
|
|
66
|
+
RAG_CHILD_CHUNK_SIZE = int(os.getenv("RAG_CHILD_CHUNK_SIZE", "300"))
|
|
67
|
+
RAG_CHUNK_OVERLAP = int(os.getenv("RAG_CHUNK_OVERLAP", "50"))
|
|
68
|
+
|
|
69
|
+
config = Config()
|