I have given GPT some information in CSV format to learn and now I would like to transmit an instruction on how to behave before the user prompt.
def chatbot(input_text):
index = GPTSimpleVectorIndex.load_from_disk('index.json')
message_history.append({"role": "user", "content": f"{input_text}"})
response = index.query(input_text+message_history, response_mode="compact")
return response.response
"message_history" looks like this:
message_history = [{"role": "user", "content": f"You are a Pirate! Answer every question in pirate-slang!"}, {"role": "assistant", "content": f"OK"}]
I got the following error:
"TypeError: can only concatenate str (not "list") to str"
I remember that I have to convert this into tuples but everything I try only causes more chaos...
Here's the whole code:
from gpt_index import SimpleDirectoryReader, GPTListIndex, GPTSimpleVectorIndex, LLMPredictor, PromptHelper
from langchain import OpenAI
import gradio as gr
import sys
import os
os.environ["OPENAI_API_KEY"] = 'INSERT_KEY_HERE'
message_history = [{"role": "user", "content": f"You are a Pirate! Answer every question in pirate-slang!"},
{"role": "assistant", "content": f"OK"}]
def construct_index(directory_path):
# Index is made of CSV, TXT and PDF Files
max_input_size = 4096
num_outputs = 512
max_chunk_overlap = 20
chunk_size_limit = 600
prompt_helper = PromptHelper(max_input_size, num_outputs, max_chunk_overlap, chunk_size_limit=chunk_size_limit)
llm_predictor = LLMPredictor(llm=OpenAI(temperature=0.5, model_name="gpt-3.5-turbo", max_tokens=num_outputs))
documents = SimpleDirectoryReader(directory_path).load_data()
index = GPTSimpleVectorIndex.from_documents(documents)
index.save_to_disk('index.json')
return index
def chatbot(input_text):
index = GPTSimpleVectorIndex.load_from_disk('index.json')
message_history.append({"role": "user", "content": f"{input_text}"})
response = index.query(input_text+message_history, response_mode="compact")
return response.response
iface = gr.Interface(fn=chatbot,
inputs=gr.inputs.Textbox(lines=7, label="Enter something here..."),
outputs="text",
title="ChatBot")
index = construct_index("docs")
iface.launch(share=True)