Spaces:

npc0 commited on
Commit
c1016d9
1 Parent(s): 411dbf0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +136 -20
app.py CHANGED
@@ -1,33 +1,149 @@
1
  import tempfile
2
  import gradio as gr
3
  import janus_swi as janus
 
 
 
 
 
4
  import nest_asyncio
5
 
6
  nest_asyncio.apply()
7
 
8
- def yes_man(message, history):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
  janus.consult("knowledge_base.pl")
10
- # tmp = tempfile.NamedTemporaryFile(suffix='.pl')
11
-
12
- # Open the file for writing.
13
- # with open(tmp.name, 'w') as f:
14
- with open("tmp.pl", 'w') as f:
15
- f.write("""% Define the person
16
- us_citizen(john_doe)
17
- lawfully_residing(john_doe, 'U.S.', date(1996, 1, 1))
18
- condition(john_doe, 'Blind')""")
19
- # janus.consult(tmp.name)
20
  janus.consult("tmp.pl")
21
- if message.endswith("?"):
22
- result = str(janus.query_once("eligible_for_ssi(john_doe)"))
 
 
 
23
  else:
24
- result = '\n- '.join([str(r) for r in janus.query("eligible_for_ssi(john_doe)")])
25
- # tmp.close()
26
- return result
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27
 
28
  gr.ChatInterface(
29
  yes_man,
30
- title="Yes Man",
31
- description="Ask Yes Man any question",
32
- examples=["Hello", "Am I cool?", "Are tomatoes vegetables?"],
33
- ).launch()
 
1
  import tempfile
2
  import gradio as gr
3
  import janus_swi as janus
4
+ from crewai import Agent, Task, Crew
5
+ from crewai_tools import tool
6
+ from crewai_tools import MDXSearchTool
7
+ from crewai_tools import WebsiteSearchTool
8
+ from langchain_anthropic import ChatAnthropic
9
  import nest_asyncio
10
 
11
  nest_asyncio.apply()
12
 
13
+ MODEL_NAME = "claude-3-5-sonnet-20240620"
14
+ llm = ChatAnthropic(model=MODEL_NAME,
15
+ temperature=0.2,
16
+ max_tokens=4096,)
17
+
18
+ webs_tool = WebsiteSearchTool(
19
+ website=DOC_URL,
20
+ config=dict(
21
+ llm=dict(
22
+ provider="anthropic",
23
+ config=dict(
24
+ model=MODEL_NAME,
25
+ temperature=0.2,
26
+ # top_p=1,
27
+ # stream=true,
28
+ ),
29
+ ),
30
+ embedder=dict(
31
+ provider="ollama",
32
+ config=dict(
33
+ model="mxbai-embed-large",
34
+ # task_type="retrieval_document",
35
+ # title="Embeddings",
36
+ ),
37
+ ),
38
+ )
39
+ )
40
+ docs_tool = MDXSearchTool(
41
+ mdx='agent_doc.md',
42
+ config=dict(
43
+ llm=dict(
44
+ provider="anthropic",
45
+ config=dict(
46
+ model=MODEL_NAME,
47
+ temperature=0.2,
48
+ # top_p=1,
49
+ # stream=true,
50
+ ),
51
+ ),
52
+ embedder=dict(
53
+ provider="ollama",
54
+ config=dict(
55
+ model="mxbai-embed-large",
56
+ # task_type="retrieval_document",
57
+ # title="Embeddings",
58
+ ),
59
+ ),
60
+ )
61
+ )
62
+
63
+ @tool("Prolog Query Engine")
64
+ def prolog_query_engine(code: str, query: str) -> str:
65
+ """Executes a Prolog query with additional Prolog code defining predicates and facts, and returns the results.
66
+
67
+ Args:
68
+ code: Prolog code defining predicates and facts. This code will be appended to knowledge base before executing the query.
69
+ query: The Prolog query to execute.
70
+
71
+ Returns:
72
+ A string containing the results of the query, with each result on a new line. If the query fails, returns "False".
73
+ """
74
  janus.consult("knowledge_base.pl")
75
+
76
+ # Remove code block markers if present
77
+ if '```' in code:
78
+ code = code.split('```')[1].split('```')[0]
79
+
80
+ # Write the provided Prolog code to a temporary file
81
+ with open('tmp.pl', 'w') as f:
82
+ f.write(code)
83
+
84
+ # Consult the temporary file to load the provided Prolog code
85
  janus.consult("tmp.pl")
86
+
87
+ # Execute the query and return the results
88
+ results = janus.query(query)
89
+ if results:
90
+ return '\n'.join([str(r) for r in results])
91
  else:
92
+ return "False"
93
+
94
+ # Define your agents with roles, goals, and tools
95
+ programmer = Agent(
96
+ role='Software Engineer',
97
+ goal='Write Prolog code and a line of Prolog queries to answer user queries',
98
+ backstory='''A software engineer with expertise in logic programming and experience using Prolog.
99
+ Can translate user requests into Prolog code and execute queries to provide accurate results.
100
+ Familiar with various Prolog concepts like recursion, backtracking, and unification.''',
101
+ tools=[prolog_query_engine, docs_tool],
102
+ llm=llm
103
+ )
104
+ consultant = Agent(
105
+ role='Consultant',
106
+ goal='Answer user query and explain in simple English that even 8 year old kid can understand',
107
+ backstory='''A friendly and patient consultant, skilled at explaining complex topics in a clear and simple way.
108
+ Can understand the output of a software engineer and translate it into easy-to-understand explanations,
109
+ even for someone as young as eight years old. Use simple words and examples to make learning fun and engaging.''',
110
+ tools=[webs_tool],
111
+ llm=llm
112
+ )
113
+
114
+ # Define a task
115
+ task1 = Task(
116
+ name='Answer user query',
117
+ description='Given a user query, write Prolog defining predicates and facts in query and build a Prolog query to access knowledge base and answer user query.\nUser query: {query}',
118
+ agent=programmer,
119
+ expected_output='''A report including:\n\
120
+ - User query\n\
121
+ - Prolog code with predicates and facts\n\
122
+ - Prolog query used to answer the user query\n\
123
+ - Result of running the Prolog query\n\
124
+ - A basic explanation of the result, clarifying how the Prolog query produced the answer'''
125
+ )
126
+ # Define a task
127
+ task2 = Task(
128
+ name='Reply user query',
129
+ description='Given answer to user query, improve the wordings in answer using your knowledge.',
130
+ agent=consultant,
131
+ expected_output='A clear, concise, and easy-to-understand explanation of the answer to the user query, suitable for an 8-year-old.'
132
+ )
133
+
134
+ # Create a crew
135
+ crew = Crew(
136
+ agents=[programmer, consultant],
137
+ tasks=[task1, task2],
138
+ verbose=True)
139
+
140
+
141
+ def yes_man(user_query, history):
142
+ return crew.kickoff(inputs={"query": user_query})
143
 
144
  gr.ChatInterface(
145
  yes_man,
146
+ title="SSI/SSDI expert",
147
+ description="Ask expert system any question",
148
+ examples=["Is it eligible for a blind US citizen born in 1996 Jan 2 name John Doe to get SSI?"],
149
+ ).queue().launch()