8
8
import pickle
9
9
import weakref
10
10
from datetime import datetime
11
+
11
12
# Ensure necessary imports for ArcanAgent
12
13
from tempfile import TemporaryDirectory
13
14
from typing import Any , AsyncIterator , Dict , List , Optional , cast
14
15
15
16
from fastapi import Depends
16
17
from fastapi .responses import StreamingResponse
17
- from langchain .agents import (AgentExecutor , AgentType ,
18
- create_tool_calling_agent , initialize_agent ,
19
- load_tools )
18
+ from langchain .agents import (
19
+ AgentExecutor ,
20
+ AgentType ,
21
+ create_tool_calling_agent ,
22
+ initialize_agent ,
23
+ load_tools ,
24
+ )
20
25
from langchain .agents .agent_types import AgentType
21
- from langchain .agents .format_scratchpad .openai_tools import \
22
- format_to_openai_tool_messages
26
+ from langchain .agents .format_scratchpad .openai_tools import (
27
+ format_to_openai_tool_messages ,
28
+ )
23
29
from langchain .agents .format_scratchpad .tools import format_to_tool_messages
24
30
from langchain .agents .output_parsers .tools import ToolsAgentOutputParser
25
31
from langchain .embeddings .openai import OpenAIEmbeddings
26
32
from langchain .memory import ConversationBufferMemory
27
33
from langchain .pydantic_v1 import BaseModel
28
34
from langchain .sql_database import SQLDatabase
29
- from langchain_community .agent_toolkits import (FileManagementToolkit ,
30
- SQLDatabaseToolkit )
35
+ from langchain_community .agent_toolkits import FileManagementToolkit , SQLDatabaseToolkit
31
36
from langchain_core .callbacks import CallbackManagerForChainRun
32
- from langchain_core .load .serializable import (Serializable ,
33
- SerializedConstructor ,
34
- SerializedNotImplemented )
37
+ from langchain_core .load .serializable import (
38
+ Serializable ,
39
+ SerializedConstructor ,
40
+ SerializedNotImplemented ,
41
+ )
35
42
from langchain_core .messages import AIMessage , HumanMessage , SystemMessage
36
43
from langchain_core .prompts import ChatPromptTemplate
44
+
37
45
# from langchain_core.pydantic_v1 import BaseModel
38
- from langchain_core .runnables import (ConfigurableField , ConfigurableFieldSpec ,
39
- Runnable , RunnableConfig ,
40
- RunnablePassthrough ,
41
- RunnableSerializable )
46
+ from langchain_core .runnables import (
47
+ ConfigurableField ,
48
+ ConfigurableFieldSpec ,
49
+ Runnable ,
50
+ RunnableConfig ,
51
+ RunnablePassthrough ,
52
+ RunnableSerializable ,
53
+ )
42
54
from langchain_core .runnables .base import Runnable , RunnableBindingBase
43
- from langchain_core .runnables .utils import (AddableDict , AnyConfigurableField ,
44
- ConfigurableField ,
45
- ConfigurableFieldSpec , Input ,
46
- Output , create_model ,
47
- get_unique_config_specs )
55
+ from langchain_core .runnables .utils import (
56
+ AddableDict ,
57
+ AnyConfigurableField ,
58
+ ConfigurableField ,
59
+ ConfigurableFieldSpec ,
60
+ Input ,
61
+ Output ,
62
+ create_model ,
63
+ get_unique_config_specs ,
64
+ )
48
65
from langchain_openai import ChatOpenAI , OpenAIEmbeddings
49
66
from pydantic import BaseModel , Field
50
67
from sqlalchemy .dialects .postgresql import insert
56
73
from arcan .ai .llm import LLM
57
74
from arcan .ai .parser import ArcanOutputParser
58
75
from arcan .ai .prompts import arcan_prompt , spells_agent_prompt
76
+
59
77
# from arcan.ai.router import semantic_layer
60
78
from arcan .ai .tools import tools as spells
79
+
61
80
# from arcan.api.session import ArcanSession
62
81
# from arcan.ai.agents import ArcanAgent
63
82
from arcan .datamodel .chat_history import ChatHistory
@@ -73,7 +92,7 @@ class ArcanAgent(RunnableSerializable):
73
92
chat_history : List = Field (default_factory = list )
74
93
user_id : Optional [str ] = None
75
94
verbose : bool = False
76
- prompt : ChatPromptTemplate = spells_agent_prompt ,
95
+ prompt : ChatPromptTemplate = ( spells_agent_prompt ,)
77
96
configs : List [ConfigurableFieldSpec ] = Field (default_factory = list )
78
97
llm_with_tools : LLM = Field (default_factory = lambda : LLM ().llm )
79
98
agent : Runnable = Field (default_factory = RunnablePassthrough )
@@ -82,22 +101,38 @@ class ArcanAgent(RunnableSerializable):
82
101
83
102
class Config :
84
103
arbitrary_types_allowed = True
85
- extra = 'allow' # This allows additional fields not explicitly defined
86
-
87
- def __init__ (self , llm = None , tools : list = spells , prompt : ChatPromptTemplate = spells_agent_prompt ,
88
- agent_type = "arcan_spells_agent" , chat_history : list = [],
89
- user_id : str = None , verbose : bool = False , configs : list = [],
90
- ** kwargs ):
91
- super ().__init__ (tools = tools , agent_type = agent_type , chat_history = chat_history ,
92
- user_id = user_id , verbose = verbose , prompt = prompt , configs = configs , ** kwargs )
93
- object .__setattr__ (self , '_llm' , llm or LLM ().llm )
104
+ extra = "allow" # This allows additional fields not explicitly defined
105
+
106
+ def __init__ (
107
+ self ,
108
+ llm = None ,
109
+ tools : list = spells ,
110
+ prompt : ChatPromptTemplate = spells_agent_prompt ,
111
+ agent_type = "arcan_spells_agent" ,
112
+ chat_history : list = [],
113
+ user_id : str = None ,
114
+ verbose : bool = False ,
115
+ configs : list = [],
116
+ ** kwargs ,
117
+ ):
118
+ super ().__init__ (
119
+ tools = tools ,
120
+ agent_type = agent_type ,
121
+ chat_history = chat_history ,
122
+ user_id = user_id ,
123
+ verbose = verbose ,
124
+ prompt = prompt ,
125
+ configs = configs ,
126
+ ** kwargs ,
127
+ )
128
+ object .__setattr__ (self , "_llm" , llm or LLM ().llm )
94
129
# Initialize other fields after the main Pydantic initialization
95
130
self .session : ArcanSession = ArcanSession ()
96
131
self .bare_tools = load_tools (["llm-math" ], llm = self .llm )
97
132
self .agent_tools = self .tools + self .bare_tools
98
133
self .llm_with_tools = self .llm .bind_tools (self .agent_tools )
99
134
self .agent , self .runnable = self .get_or_create_agent (self .user_id )
100
-
135
+
101
136
@property
102
137
def llm (self ):
103
138
return self ._llm
@@ -238,8 +273,12 @@ def invoke(
238
273
]
239
274
)
240
275
try :
241
- self .session .store_message (user_id = self .user_id , body = user_content , response = response ['output' ])
242
- self .session .store_chat_history (user_id = self .user_id , agent_history = self .chat_history )
276
+ self .session .store_message (
277
+ user_id = self .user_id , body = user_content , response = response ["output" ]
278
+ )
279
+ self .session .store_chat_history (
280
+ user_id = self .user_id , agent_history = self .chat_history
281
+ )
243
282
except SQLAlchemyError as e :
244
283
self .session .database .rollback ()
245
284
print (f"Error storing conversation in database: { e } " )
0 commit comments