ursa-ai 0.7.0rc1__py3-none-any.whl → 0.7.1__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.

Potentially problematic release.


This version of ursa-ai might be problematic. Click here for more details.

@@ -0,0 +1,58 @@
1
+ from typing import Annotated, Any, Mapping
2
+
3
+ from langchain_core.language_models import BaseChatModel
4
+ from langchain_openai import ChatOpenAI
5
+ from langgraph.graph import StateGraph
6
+ from langgraph.graph.message import add_messages
7
+ from typing_extensions import TypedDict
8
+
9
+ from .base import BaseAgent
10
+
11
+
12
+ class ChatState(TypedDict):
13
+ messages: Annotated[list, add_messages]
14
+ thread_id: str
15
+
16
+
17
+ class ChatAgent(BaseAgent):
18
+ def __init__(
19
+ self, llm: str | BaseChatModel = "openai/gpt-4o-mini", **kwargs
20
+ ):
21
+ super().__init__(llm, **kwargs)
22
+ self._build_graph()
23
+
24
+ def _response_node(self, state: ChatState) -> ChatState:
25
+ res = self.llm.invoke(
26
+ state["messages"], {"configurable": {"thread_id": self.thread_id}}
27
+ )
28
+ return {"messages": [res]}
29
+
30
+ def _build_graph(self):
31
+ graph = StateGraph(ChatState)
32
+ self.add_node(graph, self._response_node)
33
+ graph.set_entry_point("_response_node")
34
+ graph.set_finish_point("_response_node")
35
+ self._action = graph.compile(checkpointer=self.checkpointer)
36
+
37
+ def _invoke(
38
+ self, inputs: Mapping[str, Any], recursion_limit: int = 1000, **_
39
+ ):
40
+ config = self.build_config(
41
+ recursion_limit=recursion_limit, tags=["graph"]
42
+ )
43
+ return self._action.invoke(inputs, config)
44
+
45
+
46
+ def main():
47
+ model = ChatOpenAI(
48
+ model="gpt-4o", max_tokens=10000, timeout=None, max_retries=2
49
+ )
50
+ websearcher = ChatAgent(llm=model)
51
+ problem_string = "What is your name?"
52
+ print("Prompt: ", problem_string)
53
+ result = websearcher.invoke(problem_string)
54
+ return result["messages"][-1].content
55
+
56
+
57
+ if __name__ == "__main__":
58
+ print("Response: ", main())