Skip to content

Instantly share code, notes, and snippets.

@wangjohn
Created January 9, 2025 17:57
Show Gist options
  • Select an option

  • Save wangjohn/eda173e20c1825f1b2ece854e3606cc4 to your computer and use it in GitHub Desktop.

Select an option

Save wangjohn/eda173e20c1825f1b2ece854e3606cc4 to your computer and use it in GitHub Desktop.
type CitedSourceCleaner struct{}
func (c CitedSourceCleaner) Clean(ctx context.Context, message string) (string, []ResponseDetails, error) {
sourceRegex := regexp.MustCompile(`\[(Source|Ref):\s*([^\]]+)\]`)
var citations []ResponseDetails
matches := sourceRegex.FindAllStringSubmatch(message, -1)
for i, match := range matches {
citations = append(citations, ResponseDetails{
DetailType: "citation",
Content: map[string]interface{}{
"number": i + 1,
"source": match[2],
},
})
message = strings.Replace(message, match[0],
fmt.Sprintf("[%d]", i+1), 1)
}
return message, citations, nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment