Created
January 9, 2025 17:57
-
-
Save wangjohn/eda173e20c1825f1b2ece854e3606cc4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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