This repository has been archived by the owner on Oct 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 65
/
simple_usage.py
55 lines (42 loc) · 2.25 KB
/
simple_usage.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#!/bin/env python3
"""
This script describes a simple usage of the library.
You can see a breakdown of the individual steps in the README.md file.
"""
from chatgpt_memory.datastore import RedisDataStore, RedisDataStoreConfig
## set the following ENVIRONMENT Variables before running this script
# Import necessary modules
from chatgpt_memory.environment import OPENAI_API_KEY, REDIS_HOST, REDIS_PASSWORD, REDIS_PORT
from chatgpt_memory.llm_client import ChatGPTClient, ChatGPTConfig, EmbeddingClient, EmbeddingConfig
from chatgpt_memory.memory import MemoryManager
# Instantiate an EmbeddingConfig object with the OpenAI API key
embedding_config = EmbeddingConfig(api_key=OPENAI_API_KEY)
# Instantiate an EmbeddingClient object with the EmbeddingConfig object
embed_client = EmbeddingClient(config=embedding_config)
# Instantiate a RedisDataStoreConfig object with the Redis connection details
redis_datastore_config = RedisDataStoreConfig(
host=REDIS_HOST,
port=REDIS_PORT,
password=REDIS_PASSWORD,
)
# Instantiate a RedisDataStore object with the RedisDataStoreConfig object
redis_datastore = RedisDataStore(config=redis_datastore_config)
# Instantiate a MemoryManager object with the RedisDataStore object and EmbeddingClient object
memory_manager = MemoryManager(datastore=redis_datastore, embed_client=embed_client, topk=1)
# Instantiate a ChatGPTConfig object with the OpenAI API key and verbose set to True
chat_gpt_config = ChatGPTConfig(api_key=OPENAI_API_KEY, verbose=False)
# Instantiate a ChatGPTClient object with the ChatGPTConfig object and MemoryManager object
chat_gpt_client = ChatGPTClient(config=chat_gpt_config, memory_manager=memory_manager)
# Initialize conversation_id to None
conversation_id = None
# Start the chatbot loop
while True:
# Prompt the user for input
user_message = input("\n \033[92m Please enter your message: ")
# Use the ChatGPTClient object to generate a response
response = chat_gpt_client.converse(message=user_message, conversation_id=None)
# Update the conversation_id with the conversation_id from the response
conversation_id = response.conversation_id
print("\n \033[96m Assisstant: " + response.chat_gpt_answer)
# Print the response generated by the chatbot
# print(response.chat_gpt_answer)