Here’s a new Model Documentation for start_of_turn model formats!
This is the model released by Google yesterday called Gemma
You can replace the api url with any of these model endpoints
API_URL = “https://api-inference.huggingface.co/models/google/gemma-2b-it”
API_URL = “https://api-inference.huggingface.co/models/google/gemma-7b-it”
API_URL = “https://api-inference.huggingface.co/models/google/gemma-2b”
API_URL = “https://api-inference.huggingface.co/models/google/gemma-7b”
local BearerID="Get your Free API key from Huggingface"
function cm.Gemma(systemMessage, query, previousConversation)
-- Define the API URL and headers
local API_URL = "https://api-inference.huggingface.co/models/google/gemma-2b-it"
local headers = {Authorization = BearerID}
-- Define a function to query the API with a payload
local function querys(payload)
-- Use HttpService to make a POST request
local response = httpService:PostAsync(API_URL, httpService:JSONEncode(payload), Enum.HttpContentType.ApplicationJson, false, headers)
-- Return the response as a Lua table
return httpService:JSONDecode(response)
end
-- Define a function to generate a chat response using the API and a chat template
--local function chat(systemMessage, query, previousConversation)
-- Initialize an empty table to store the conversation history
local conversation = {}
-- Chat Start with system message
table.insert(conversation, "<start_of_turn>system\n" .. systemMessage .. "<end_of_turn>")
-- Append the previous conversation to the table, if any
if previousConversation then
local everyother=0 --assume first entry is user input
for key, line in previousConversation do
if everyother==0 then
everyother=1
table.insert(conversation, "<start_of_turn>user\n" .. line .. "<end_of_turn>")
else
everyother=0
table.insert(conversation, "<start_of_turn>model\n" .. line .. "<end_of_turn>")
end
end
end
-- Append the system message to the table, using the chat template
-- Append the user query to the table, using the chat template
table.insert(conversation, "<start_of_turn>user\n" .. query .. "<end_of_turn>")
table.insert(conversation, "<start_of_turn>model\n")--provide a line for the model
-- Query the API with the conversation table as the input
local output = querys({inputs = table.concat(conversation, "\n")})
-- Extract the generated text from the output
print(output)
local generatedText = output[1]["generated_text"]
print(generatedText)
local lines = {}
for line in string.gmatch(generatedText, "[^\n]+") do
table.insert(lines, line)
end
print(lines)
-- Loop through the lines in reverse order
local result = ""
for i = #lines, 1, -1 do
-- Check if the line starts with <start_of_turn>model
if lines[i]=="<start_of_turn>model" then
-- Remove the <start_of_turn>model tag from the line
-- local line = lines[i]
local lastline=#lines
for i2 = #lines, 1, -1 do
if i==i2 then break
else
local line = lines[i2]
result = line .. "\n" .. result
end
end
break
-- Concatenate the line to the result
end
end
print(result)
-- local result=string.sub(generatedText, startIndex + 20, endIndex - 1)
if result then
-- Check if the result ends with <end_of_turn>
if string.sub(result, -12) == "<end_of_turn>" then
-- Remove the <end_of_turn> tag from the result
result = string.sub(result, 1, -13)
end
if previousConversation==nil then
previousConversation={}
end
table.insert(previousConversation,query)
table.insert(previousConversation,result)
end
return result,previousConversation
end
You can test this function in a module using the command line in studio
local cm=require(game.ReplicatedStorage.GlobalSpells.ChatbotAlgorithm.ChatModule) print(cm.Gemma("You are a helpful chatbot", "What is the best way to make eggs for breakfast?", nil))
This returns the conversation and the result
[1] = "What is the best way to make eggs for breakfast?",
[2] = "**How to Make Perfect Hard-Boiled Eggs:**
**Ingredients:**
* 1 large egg
* 1 tablespoon water
* Seasoning (optional)
**Instructions:**
1. **Separate the egg into two portions.** Use a spoon to remove the yolk and place it in a bowl. Keep the membrane with the yolk.
2. **Add the water to a saucepan.** Bring the water to a boil over medium-high heat.
3. **Gently lower```