Dim question As String
Dim LLM As ABLLM
' We are going to use RAG (Retrieval Augmented Generation) in the chain.
' LLMs can reason about wide-ranging topics, but their knowledge is limited to the public data up to a specific point in time
' that they were trained on. If you want to build AI applications that can reason about private data or data introduced after
' a model's cutoff date, you need to augment the knowledge of the model with the specific information it needs.
LLM.Initialize(True)
' the OpenAI keys
LLM.OPENAI_APIKEY = "sk-proj-Qus6to4w01WofSs6z0_2g3tN4T7ekRkWYWuS3..."
LLM.OPENAI_ORGANIZATIONID = "org-OeNM4oDm..."
' path where to store the Vecktorized documents
LLM.STORE_PATH = "K:/ABLLMStore"
' some other parameters can be set (these are the defaults)
LLM.LOG_REQUESTS = False
LLM.LOG_RESPONSES = False
LLM.OPENAI_MODELNAME = "gpt-4o"
LLM.OPENAI_IMAGEMODELNAME = "dall-e-3"
LLM.OPENAI_TEMPERATURE = 0.7
LLM.OPENAI_MAXTOKENS = 512
LLM.RAG_MAX_RESULTS = 3
LLM.RAG_MIN_SCORE = 0.7
LLM.RESPONSE_MAX_MESSAGES = 3
LLM.SPLITTER_SEGMENT_SIZE = 300
LLM.SPLITTER_SEGMENT_OVERLAP = 0
Dim NoRAGTokens As Long
' adding two documents, one about The Matrix and one about BANano
' the RAG router will only use segments the ones that are relevant to the question, reducing the input tokens significantly.
' You pay OpenAI for the number of tokens you input and output so reducing them is important
NoRAGTokens = NoRAGTokens + LLM.AddDocument("https://www.onetwowork.com/usecases", "description of use cases of One Two Work", "Work2020", False) ' the file is 108KB, or about 26.562 tokens.
NoRAGTokens = NoRAGTokens + LLM.AddDocument("K:\TheMatrix.txt", "description of the movie The Matrix", "Matrix", False) ' the file is 108KB, or about 26.562 tokens.
NoRAGTokens = NoRAGTokens + LLM.AddDocument("K:\BANanoEssentialsV1.09.pdf", "description of the programming language BANano for B4J", "BANano", False) ' the file is 390KB, or about 92.971 tokens.
NoRAGTokens = NoRAGTokens + LLM.AddDocument("K:\TeamCup-Ranking.xlsx", "scoring of the Team cup", "TeamCup", False)
NoRAGTokens = NoRAGTokens + LLM.AddDocument("K:\OpenAISources\B4xBasicLanguageV1_2.docx", "description of the B4J language", "B4J",False)
' load the store
NoRAGTokens = LLM.LoadStore
' you can add tools to the chain that do tasks or return extra information to the LLM in the chain.
Dim MyTools As AllMyTools
MyTools.Initialize
' the parameters we want the LLM to retun to our function. They are always in json format.
Dim ParamDescriptions As Map
ParamDescriptions.Initialize
ParamDescriptions.Put("MovieName", "the name of the movie")
ParamDescriptions.Put("Year", "the year the movie was released if it is known")
ParamDescriptions.Put("BoxOffice", "the box office results if it is known")
' describe what the tool does. If it is relevant to the question, it will be used.
LLM.AddTool(MyTools, "MyMovieTool", ParamDescriptions, "saves some details about the movie in a database")
' start the LLM chain
LLM.Start
Log("Chain started...")
Dim AllTokens As List
AllTokens.Initialize
' describe an image
' question = "What do you see?"
question = "Read barcodes"
Log("QUESTION: " & question)
Log("------------------------------------------------------------------------")
Dim result As ABLLMResult = LLM.QueryImage(question, "K:\barcodes.png", False)
Dim VisionTokens As TokensUsed = LogResult(result, False)
If VisionTokens.Input > 0 Then
AllTokens.Add(VisionTokens)
End If
' generate an image
question = "Donald Duck in New York, cartoon style"
Log("QUESTION: " & question)
Log("------------------------------------------------------------------------")
Dim result As ABLLMResult = LLM.GenerateImage(question, False)
Dim ImageTokens As TokensUsed = LogResult(result, False)
If ImageTokens.Input > 0 Then
AllTokens.Add(ImageTokens)
End If
question = "Summarize the use cases of One Two Work"
Log("QUESTION: " & question)
Log("------------------------------------------------------------------------")
Dim result As ABLLMResult = LLM.Chat(question, True, False)
Dim Q1Tokens As TokensUsed = LogResult(result, False)
If Q1Tokens.Input > 0 Then
AllTokens.Add(Q1Tokens)
End If
' this will only use an extract of the text segments (by using the RAG) of The Matrix file
' reducing Token cost significantly. Because we ask it to save some details, the MyMovieTool function will be called.
question = "What is the plot of the movie The Matrix and save the details."
'question = "Give Matrix (film) plot and save"
Log("QUESTION: " & question)
Log("------------------------------------------------------------------------")
Dim result As ABLLMResult = LLM.Chat(question, True, False)
Dim Q2Tokens As TokensUsed = LogResult(result, False)
If Q2Tokens.Input > 0 Then
AllTokens.Add(Q2Tokens)
End If
' this will only use an extract of the text segments (by using the RAG) of the BANano file
' reducing Token cost significantly
question = "What is BANano?"
Log("QUESTION: " & question)
Log("------------------------------------------------------------------------")
Dim result As ABLLMResult = LLM.Chat(question, True, False)
Dim Q3Tokens As TokensUsed = LogResult(result, False)
If Q3Tokens.Input > 0 Then
AllTokens.Add(Q3Tokens)
End If
Dim TotalINTokens As Long
Dim TotalOUTTokens As Long
Dim NumOfRAGQuestions As Long = AllTokens.Size
For i = 0 To NumOfRAGQuestions - 1
Dim tmpTokens As TokensUsed = AllTokens.Get(i)
TotalINTokens = TotalINTokens + tmpTokens.Input
TotalOUTTokens = TotalOUTTokens + tmpTokens.Output
Next
Dim NoRAG As Long = NoRAGTokens * NumOfRAGQuestions + TotalOUTTokens
Dim WithRAG As Long = TotalINTokens * NumOfRAGQuestions + TotalOUTTokens
Dim PriceNoRAG As Double = (NoRAGTokens * NumOfRAGQuestions * 5.0/1000000.0) + (TotalOUTTokens * 15.0/1000000.0)
Dim PriceWithRAG As Double = (TotalINTokens * 5.0/1000000.0) + (TotalOUTTokens * 15.0/1000000.0)
LogError("Without RAG, asking these " & NumOfRAGQuestions & " questions would've used " & NoRAG & " of our allowed tokens, cost: $" & PriceNoRAG)
LogError("With RAG, asking these " & NumOfRAGQuestions & " questions only used " & WithRAG & " our allowed tokens, cost: $" & PriceWithRAG)