Comments (3)
We have an internal truncateRequest
function to resolve this on our instance. It tries to take as many tokens as possible up until about half the token limit. This is super simple implementation that we threw together in under an hour.
func getSubstring(message string, characters int) string {
if len(message) < characters {
return message
}
return message[:characters/2] + message[len(message)-characters/2:]
}
func (s *OpenAI) truncateRequest(request openaiClient.ChatCompletionRequest) openaiClient.ChatCompletionRequest {
var messages []openaiClient.ChatCompletionMessage
token_count := 0
limit := s.TokenLimit() / 2
for i := len(request.Messages) - 1; i >= 0; i-- {
message := request.Messages[i]
// Add a few for the role, etc.
tokens := s.CountTokens(message.Content) + 10
// Can we fit the entire message in the window?
if token_count+tokens < limit {
token_count += tokens
messages = append([]openaiClient.ChatCompletionMessage{message}, messages...)
continue
}
// We can't fit the message in, so just include the start and the end
remaining := limit - token_count
characters := remaining * 4 // Estimate 4 characters per token
message.Content = getSubstring(message.Content, characters)
messages = append(messages, message)
break
}
request.Messages = messages
return request
}
from mattermost-plugin-ai.
This makes a lot of sense as it's a pretty bad experience when you run oft of context at the moment. The annoying part is (at least last time I checked) there is no precise token counting library in Go.
from mattermost-plugin-ai.
We've had a lot of success with fairly basic heuristics. We've generated millions of values and using the existing built-in token counter has worked every time. I realise we're possibly losing a small amount of the context window, but the trade-off is worthwhile for us.
from mattermost-plugin-ai.
Related Issues (20)
- 📑 docs: Update README and issues ahead of v1.0 release
- Webapp bad export of AdvancedCreateComment
- 🐛 bug: Wrong file added to the 0.5.0 pre release HOT 1
- 💡 idea: Ollama support HOT 7
- 🐛 bug: Not able to active in Mattermost V6&V7 HOT 3
- 📑 docs: adjust v1.0 feature documentation
- 💡 idea: Add Organization Input Option to Open AI Engine Configuration Form
- 💡 idea: Is it possible to make it work properly without setting up the github plugin? HOT 1
- 🐛 bug: not work with LocalAI backend HOT 10
- 🐛 bug: Anthropic model integration Issue: Header requirement error and failed responses HOT 5
- 🐛 bug: deleting suggested prompt prevents me from using another suggested prompt
- 💡 idea: add supported Mattermost server versions to README
- 🐛 bug: When replying in a thread, the response from OpenAI is not updated on the main screen in the stream.
- Why Does My AI Reply to My Threads Instead of Posting Directly in the Channel? HOT 5
- 💡 idea: add google gemini to list of supported backends HOT 2
- 💡 idea: Update README header image to say "Mattermost AI Copilot" instead of "The Mattermost AI Plugin" HOT 1
- 💡 idea: remove duplicative docs HOT 1
- 💡 idea: add a data-preloaded Mattermost server to facilitate demos and testing
- 💡 idea: Take into account the user locale for AI actions HOT 8
- 💡 idea: Soften the Enterprise requirement for multiple LLM backends (Add AI Service) HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from mattermost-plugin-ai.