diff --git a/examples/example.py b/examples/example.py index 165b78b7..9421283a 100644 --- a/examples/example.py +++ b/examples/example.py @@ -35,7 +35,9 @@ async def main(): time.sleep(10) chat = await talky.chat("what is my name") logger.info(chat) - # Your name is Jack, as you mentioned earlier. + # Your name is Jack, as you mentioned earlier + + talky.conversation.export_messages() app = FastAPI() diff --git a/history.json b/history.json new file mode 100644 index 00000000..efa0b54f --- /dev/null +++ b/history.json @@ -0,0 +1,30 @@ +[ + { + "role": "user", + "content": "You are a friendly AI, helping me with \ngeneral tasks.\n" + }, + { + "role": "user", + "content": "My name is Jack" + }, + { + "role": "ai", + "content": "Hello Jack, this is Bing. Nice to meet you. \ud83d\ude0a" + }, + { + "role": "user", + "content": "tell me who is president of the united states?" + }, + { + "role": "ai", + "content": "[1]: https://simple.wikipedia.org/wiki/President_of_the_United_States \"President of the United States - Simple English Wikipedia, the free ...\"\n[2]: https://www.history.com/topics/us-presidents/joe-biden \"Joe Biden: Age, Presidency, Family - HISTORY\"\n[3]: https://www.whitehouse.gov/about-the-white-house/presidents/barack-obama/ \"Barack Obama | The White House\"\n[4]: https://en.wikipedia.org/wiki/List_of_Presidents_of_the_United_States \"List of presidents of the United States - Wikipedia\"\n[5]: https://www.whitehouse.gov/about-the-white-house/presidents/ \"Presidents | The White House\"\n[6]: https://www.britannica.com/topic/Presidents-of-the-United-States-1846696 \"list of presidents of the United States - Encyclopedia Britannica\"\n\nThe current president of the United States is **Joe Biden**, who took office in January 2021[^1^][1] [^2^][2]. He is the 46th president of the United States[^3^][4] [^4^][5]. Do you want to know more about him or any other president?" + }, + { + "role": "user", + "content": "what is my name" + }, + { + "role": "ai", + "content": "You told me your name is Jack. Is that correct?" + } +] \ No newline at end of file diff --git a/myllm/main.py b/myllm/main.py index 381130a6..5ce65022 100644 --- a/myllm/main.py +++ b/myllm/main.py @@ -5,6 +5,7 @@ """ import importlib +import json from time import sleep from g4f import Provider @@ -123,3 +124,7 @@ def add_message(self, role: str, content: str): def get_messages(self): return self.messages + + def export_messages(self): + with open("history.json", "w") as f: + json.dump(self.messages, f, indent=4) diff --git a/tests/test_unit.py b/tests/test_unit.py index 8ed488cd..3b09e19d 100644 --- a/tests/test_unit.py +++ b/tests/test_unit.py @@ -37,6 +37,12 @@ async def test_get_myllm_info(talky): assert "ℹ️" in result +@pytest.mark.asyncio +async def test_clear_chat_history(talky): + history = talky.conversation.export_messages() + assert history is not None + + @pytest.mark.asyncio async def test_clear_chat_history(talky): await talky.clear_chat_history()