-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add v1 of function-driven implementation for Ansari
- Loading branch information
1 parent
09846b3
commit a2dab09
Showing
8 changed files
with
132 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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?' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters