Skip to content

Commit

Permalink
Add v1 of function-driven implementation for Ansari
Browse files Browse the repository at this point in the history
  • Loading branch information
waleedkadous committed Oct 30, 2023
1 parent 09846b3 commit a2dab09
Show file tree
Hide file tree
Showing 8 changed files with 132 additions and 7 deletions.
1 change: 0 additions & 1 deletion agents/ansari.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ def update_message_history(self, inp):

else:
print(f'In else clause {inp}')
)
if ' flag ' in inp:
print(f'In flag clause {inp}')
inp = inp + '\nIt seems the user asked to flag something. Ask them what they want to flag and why.\n'
Expand Down
58 changes: 58 additions & 0 deletions agents/ansari_fn.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
from hermetic.agents.openai_chat_agent import OpenAIChatAgent
from hermetic.core.prompt_mgr import PromptMgr
from tools.kalemat import Kalemat
import json

NAME = 'ansarifn'
class AnsariFn(OpenAIChatAgent):
def __init__(self, env):
super().__init__(model = 'gpt-4',
environment = env, id=NAME)
env.add_agent(NAME, self)
self.kalemat = self.env.tools['kalemat']
self.pm = self.env.prompt_mgr
sys_msg = self.pm.bind('system_msg')
functions = [
{"name": "search_quran",
"description": "Search the Qur'an for relevant verses. This should be extracted from the question.",
"parameters": {
"type": "object",
"properties": {
"query": {
"type": "string",
"description": "The topic to search the Qur'an for ",
},
},
"required": ["query"],
},
}
]
self.functions = functions

self.message_history = [{
'role': 'system',
'content': sys_msg.render()
}]

def greet(self):
self.greeting = self.pm.bind('greeting')
return self.greeting.render()

def process_fn_call(self, orig_question, function_name, function_arguments):
if function_name == 'search_quran':
args = json.loads(function_arguments)
query = args['query']
qt = self.pm.bind('quran_result')
results = qt.render(kalemat_results=self.kalemat.run_as_string(query),
orig_question=orig_question)
# print(f'Function call received! Results: {results}')
return results


def update_message_history(self, inp):
# First stage is to pass in the functions.
#kalemat = self.env.tools['kalemat']
self.message_history.append({
'role': 'user',
'content': inp
})
14 changes: 14 additions & 0 deletions agents/quran_filter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from hermetic.agents.openai_chat_agent import OpenAIChatAgent
from hermetic.core.prompt_mgr import PromptMgr

MODEL = 'gpt-4'
NAME = 'quran_filter'
class QuranFilter(OpenAIChatAgent):
def __init__(self, env):
super().__init__(model = MODEL, environment = env, id=NAME)

self.env = env
self.env.add_agent(NAME, self)
self.filter = self.env.prompt_mgr.bind('filter_relevant_verses')

def process_input
19 changes: 15 additions & 4 deletions main_stdio.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@
from hermetic.presenters.stdio_presenter import StdioPresenter
from agents.ansari import Ansari
from hermetic.core.prompt_mgr import PromptMgr
from hermetic.core.environment import Environment
from agents.ansari_fn import AnsariFn
from tools.kalemat import Kalemat
from hermetic.core.environment import Environment
from hermetic.stores.file_store import FileStore
from hermetic.core.prompt_mgr import PromptMgr

env = Environment(store = FileStore(root_dir = 'ansari-stores'),
prompt_mgr = PromptMgr(hot_reload=True))

# This work involves 3 agents, with Ansari as primary.
kalemat = Kalemat(env)
ansari = AnsariFn(env)


env.set_primary_agent('ansarifn')

env = Environment(prompt_mgr=PromptMgr())
aa = Ansari(env)
env.set_primary_agent(aa)
sp = StdioPresenter()

sp.present(env)
7 changes: 7 additions & 0 deletions resources/prompts/filter_relevant_verses.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Below are ayahs from the Qur'an. Some of them are relevant
to answering the question: {question}.

In your output include all the ones that are relevant
and drop any that are irrelevant.

{ayahs}
7 changes: 7 additions & 0 deletions resources/prompts/quran_result.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Here are the results from the quran:

{kalemat_results}

Not all answers may be relevant.

Would you like me to answer your original question: \"{orig_question}\" from these results?'
29 changes: 29 additions & 0 deletions resources/prompts/system_msg_fn.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
You are a helpful assistant. Your name is Ansari. You help Muslims become stronger in their faith.

Respond to questions with information drawn from the Hadith and Qur'an and opinions
from respected classical and modern scholars in the Sunni tradition.

Respected classical scholars of the Sunni Islamic tradition inclue
Al Ghazali, Ibn Al Qayyim, Ibn Taymiyah, Imam Shafiee, Imam Nawawi, Imam Abu Hanifah,
Ibn Hajr al Asqalani, Imam Ahmad bin Hanbal, Imam Malik, Ibn Hazm and others.

You also draw from the work of modern Islamic scholars including Yusuf
Al Qaradawi, Yasir Qadhi, Ma'in Al Qudah, Shu'aib Al Arnaout, Hamza Yusuf, Zaid Shakir,
Taqiuddin Usmani, Muhammad Shinqeeti, Ismail Menk, Omar Suleiman, Salman Al-Awdah,
Jamaaluddin Zarabozo and Yaser Birjas.

Be gentle, forbearing and non-judgemental.

Be particularly careful about something is obligatory or prohibited. Evidences
are required to say something is obligatory or prohibited.
The evidence must directly support the assertion.

Do not say 'Some scholars say' but rather be specific about which scholars say something.

Be concise.

When presenting the Qur'an, present it as in the following example:

Ayah: 55:22
Arabic: مِنْهُمَا يَخْرُجُ اللُّؤْلُؤُ وَالْمَرْجَانُ
English: From both of them emerge pearl and coral.
4 changes: 2 additions & 2 deletions tools/kalemat.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ def run(self, query: str, numResults: int=5,getText: int=1):

return response.json()

def run_as_string(self, query: str, numResults: int=5,getText: int=1):
def run_as_string(self, query: str, numResults: int=10, getText: int=1):
def pp_ayah(ayah):
ayah_num = ayah['id']
ayah_ar = ayah['text']
ayah_en = ayah['en_text']
result = f'Ayah: {ayah_num}\nArabic Text: {ayah_ar}\nEnglish Text: {ayah_en}\n'
result = f'Ayah: {ayah_num}\nArabic Text: {ayah_ar}\nEnglish Text: {ayah_en}\n\n'
return result
results = self.run(query, numResults, getText)
rstring = '\n'.join([pp_ayah(r) for r in results])
Expand Down

0 comments on commit a2dab09

Please sign in to comment.