Skip to content

Instantly share code, notes, and snippets.

@k5trismegistus
Created September 7, 2023 15:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save k5trismegistus/30f37f6b6b241667e78894c2bbad7bbf to your computer and use it in GitHub Desktop.
Save k5trismegistus/30f37f6b6b241667e78894c2bbad7bbf to your computer and use it in GitHub Desktop.
!pip install llama-index
import openai
from llama_index import GPTVectorStoreIndex, SimpleDirectoryReader
openai.api_key = <OpenAIのAPIキー>
documents = SimpleDirectoryReader("./data-text").load_data()
index = GPTVectorStoreIndex.from_documents(documents)
index.storage_context.persist()
import sys, re
import openai
from llama_index import StorageContext, load_index_from_storage
from llama_index.memory import ChatMemoryBuffer
openai.api_key = <OpenAIのAPIキー>
print("*** myGPT ***")
print()
memory = ChatMemoryBuffer.from_defaults(token_limit=1500)
storage_context = StorageContext.from_defaults(persist_dir="./storage")
index = load_index_from_storage(storage_context)
chat_engine = index.as_chat_engine(
chat_mode="context",
memory=memory,
system_prompt="You are a chatbot, able to have normal interactions. Replay as the writer of blog '未来世紀アラカワ'",
)
def insert_newlines(text, line_length):
lines = [text[i:i + line_length] for i in range(0, len(text), line_length)]
return '\n'.join(lines).lstrip('\n')
while True:
input_text = input(">")
print()
if input_text.strip() == "":
print("exit...")
exit() # プログラムを終了する
else:
text = str(chat_engine.chat(input_text))
result = insert_newlines(text, 60)
print("myGPT>"+result)
print()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment