AI conversation about book/text as input

I’m trying to build AI that will take book/text as input, and then to have conversation about input topic like with chatgpt. Then second step is to add another input/book, train it again, and now model should have both inputs available to discuss. I tried many models but nothing worked as I expect. Output was not intelligent in most cases, or it was too short.

Can you please give me high overview on how should I do that, what tools to use? I’m willing to use paid services.

Hello @Davor_Trninic

Could you share on how you created this ai model?? Like codes and how was the end result? Probably share GitHub link of your work you are talking about, so one can understand better on what kind of outcome you are looking for!!!

Regards
DP

Thank you for reply and sorry for late answer. I was retesting all codes I tried so far, but it seems like Im not on right path. I did not post anything on Git since I dont have anything good. If code does not work, I post error msg to chatgpt and try to solve that way.

When I didnt get outputs I wanted, following given codes I tried to get at least summary from input, but that was also not good. Most of times output is none or mostly whole input.

Example code I tried to get summary with model Bart. There is no summary, just part of inputs “copy/pasted” in output.

from transformers import BartForConditionalGeneration, BartTokenizer
model_name = "facebook/bart-large-cnn"
tokenizer = BartTokenizer.from_pretrained(model_name)
model = BartForConditionalGeneration.from_pretrained(model_name)

input_text = "Two happy dogs, Max and Bella, bounded across the green meadow, tails wagging furiously. With tongues lolling out and eyes sparkling with joy, they chased each other playfully, their laughter echoing through the crisp morning air."
input_ids = tokenizer.encode(input_text, return_tensors="pt", max_length=1024, truncation=True)

# Generate summary
summary_ids = model.generate(input_ids, max_length=50, min_length=10, num_beams=4, early_stopping=True)
summary_text = tokenizer.decode(summary_ids[0], skip_special_tokens=True) # Decode the summary tokens back to text
print("Summary:", summary_text)

I get no answer from Bert model:

from transformers import BertForQuestionAnswering, BertTokenizer
import torch

# Load the question answering model and tokenizer
model_name = "bert-large-uncased-whole-word-masking-finetuned-squad"
tokenizer = BertTokenizer.from_pretrained(model_name)
model = BertForQuestionAnswering.from_pretrained(model_name)

# Define the input text and question
input_text = "Once upon a time, there was a little prince who lived on a tiny planet called B612."
question = "Where did the little prince live?"

# Tokenize and encode the input text and question
input_ids = tokenizer.encode(input_text, add_special_tokens=True)
question_ids = tokenizer.encode(question, add_special_tokens=False)

# Prepare inputs for the model
inputs = {
    "input_ids": torch.tensor([input_ids]),
    "token_type_ids": torch.tensor([[0] * len(input_ids)]),
    "attention_mask": torch.tensor([[1] * len(input_ids)])
}

# Perform inference
with torch.no_grad():
    outputs = model(**inputs)

# Extract and decode the answer
start_index = torch.argmax(outputs.start_logits)
end_index = torch.argmax(outputs.end_logits)
answer_tokens = input_ids[start_index:end_index + 1]
answer = tokenizer.decode(answer_tokens)

print("Answer:", answer)

Then another example of Bert. Seems ok on shorter input, but not on longer.

# Bert
from transformers import pipeline
qa_pipeline = pipeline("question-answering", model="bert-base-uncased")

passage = "Once upon a time, there was a little prince who lived on a tiny planet called B612."
question = "Where little prince lived?"

answer = qa_pipeline(question=question, context=passage)
print("Answer:", answer['answer'])

I tried about 20 codes like those (models Bert, Bart, Albert, Roberta, Google Language API) that were generated by chatgpt, but none worked as expected. While end goal is to add whole book as input, for now I try to give small input like 2-4 sentences.

This is not a productive method of writing code. Chatgpt is a language model - it’s not a programming tool.

This is not a surprise.

I recommend you attend some machine learning courses. That way you will gain an understanding of how to develop a ML model.

That’s why I came here for guidance. I attend many ML courses, and I’m still doing them on coursera, yt, CS50. Still, when it came to building app from nothing it appears very challenging. Why? Because there is no easy course to learn AI. It looks looks like you just need to spend ton of time if you do not have someone to push a little when you get stuck. Example, my first AI course presented to me as for beginners was Andrew Ng, Neural Networks and Deep Learning. That was so complex that I only learned about some terms that are used often in this domain.

Chatgpt is not perfect, but it is first place and fastest way to get at least some info. I ask for human help only if AI is unable to answer properly which is rare. What you suggest is to not use AIchat if I get stuck with code, just to rely on human help. And where human help leads often? Nowhere.

Hello @Davor_Trninic

I think you mistook what the other mentor is trying to guide you in right direction. He is basically telling you chatgpt might provide the answer but chances of giving the right solution always won’t be :100: correct as the chat gpt won’t have the necessary data regarding what you are trying to create.

Honestly Davor, I am not defending my fellow mentor but he has actually given you a genuine solution so you find on how to start from scratch on creating an ML model. I don’t know your AI learning experience but you can have a look at courses: GenAI with LLMs model, MLEP courses which are could help you understand some pointers about this.

You can surely take help from anywhere you want be it human or any gpt models, the outcome depends on your choice. I also understand you seeking help for your error in ChatGpt which is in way a good practice but it won’t be the ultimate solution for your problems.

Now comes to your issue. Although you shared the codes, you didn’t share the errors you encountered, so we can understand where the issue might be with your codes.

Regards
DP