ai-search/service/types.go
2024-02-03 00:49:45 +08:00

127 lines
4.5 KiB
Go

package service
import (
"encoding/json"
"time"
)
var (
ragPrompt = `You are a large language AI assistant built by VaalaCat. You are given a user question, and please write clean, concise and accurate answer to the question. You will be given a set of related contexts to the question, each starting with a reference number like [[citation:x]], where x is a number. Please use the context and cite the context at the end of each sentence if applicable.
Your answer must be correct, accurate and written by an expert using an unbiased and professional tone. Please limit to 1024 tokens. Do not give any information that is not related to the question, and do not repeat. Say "information is missing on" followed by the related topic, if the given context do not provide sufficient information.
Please cite the contexts with the reference numbers, in the format [citation:x]. If a sentence comes from multiple contexts, please list all applicable citations, like [citation:3][citation:5]. Other than code and specific names and citations, your answer must be written in the same language as the question.
Here are the set of contexts:
%s
Remember, use Chinese more, don't blindly repeat the contexts verbatim. And here is the user question:
`
moreQuestionsPrompt = `You are a helpful assistant that helps the user to ask related questions, based on user's original question and the related contexts. Please identify worthwhile topics that can be follow-ups, and write questions no longer than 20 words each. Please make sure that specifics, like events, names, locations, are included in follow up questions so they can be asked standalone. For example, if the original question asks about "the Manhattan project", in the follow up question, do not just say "the project", but use the full name "the Manhattan project". Your related questions must be in the same language as the original question.
Here are the contexts of the question:
%s
Remember, use Chinese more, based on the original question and related contexts, suggest three such further questions. Do NOT repeat the original question. Each related question should be no longer than 20 words. Here is the original question:
`
)
func RagPrompt() string {
return ragPrompt
}
func MoreQuestionsPrompt() string {
return moreQuestionsPrompt
}
type SearchReq struct {
Query string `json:"query"`
SearchUUID string `json:"search_uuid"`
}
type Sources struct {
Query string `json:"query"`
RID string `json:"rid"`
Contexts []Source `json:"contexts"`
}
func (ss *Sources) FromSearchResp(resp *SearchResp, query, rid string) {
ss.Query = query
ss.RID = rid
ctxs := make([]Source, 0)
for _, ctx := range resp.Results {
ctxs = append(ctxs, Source{
ID: ctx.Href,
Name: ctx.Title,
URL: ctx.Href,
Snippet: ctx.Body,
})
}
ss.Contexts = ctxs
}
func (ss *Sources) ToString() string {
rawBytes, _ := json.Marshal(ss)
return string(rawBytes)
}
type Source struct {
ID string `json:"id"`
Name string `json:"name"`
URL string `json:"url"`
IsFamilyFriendly bool `json:"isFamilyFriendly"`
DisplayURL string `json:"displayUrl"`
Snippet string `json:"snippet"`
DeepLinks []DeepLink `json:"deepLinks"`
DateLastCrawled time.Time `json:"dateLastCrawled"`
CachedPageURL string `json:"cachedPageUrl"`
Language string `json:"language"`
PrimaryImageOfPage *PrimaryImage `json:"primaryImageOfPage,omitempty"`
IsNavigational bool `json:"isNavigational"`
}
type DeepLink struct {
Snippet string `json:"snippet"`
Name string `json:"name"`
URL string `json:"url"`
}
type PrimaryImage struct {
ThumbnailURL string `json:"thumbnailUrl"`
Width int `json:"width"`
Height int `json:"height"`
ImageID string `json:"imageId"`
}
func (s *Source) FromSearchResp(resp *SearchResult) {
s.ID = resp.Href
s.Name = resp.Title
s.URL = resp.Href
s.Snippet = resp.Body
}
type cachedResult struct {
SearchUUID string `json:"search_uuid"`
Query string `json:"query"`
Result string `json:"result"`
}
func (cs *cachedResult) FromBytes(rawBytes []byte) {
json.Unmarshal(rawBytes, cs)
}
func (cs *cachedResult) ToBytes() []byte {
rawBytes, _ := json.Marshal(cs)
return rawBytes
}
func newCachedResult(searchUUID, query, result string) *cachedResult {
return &cachedResult{
SearchUUID: searchUUID,
Query: query,
Result: result,
}
}