|
| 1 | +package stream |
| 2 | + |
| 3 | +import ( |
| 4 | + "bufio" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "os" |
| 8 | + "strings" |
| 9 | +) |
| 10 | + |
| 11 | +type Choice struct { |
| 12 | + Delta struct { |
| 13 | + Content string `json:"content"` |
| 14 | + } `json:"delta"` |
| 15 | +} |
| 16 | + |
| 17 | +type Data struct { |
| 18 | + Choices []Choice `json:"choices"` |
| 19 | +} |
| 20 | + |
| 21 | +func ParseFile(filename string) error { |
| 22 | + // Open the file |
| 23 | + file, err := os.Open(filename) |
| 24 | + if err != nil { |
| 25 | + return fmt.Errorf("could not open file: %w", err) |
| 26 | + } |
| 27 | + defer file.Close() |
| 28 | + |
| 29 | + scanner := bufio.NewScanner(file) |
| 30 | + var contentBuilder strings.Builder |
| 31 | + |
| 32 | + for scanner.Scan() { |
| 33 | + line := scanner.Text() |
| 34 | + |
| 35 | + // Check if the line has "data: " prefix |
| 36 | + if strings.HasPrefix(line, "data: ") { |
| 37 | + // Remove the "data: " prefix |
| 38 | + line = strings.TrimPrefix(line, "data: ") |
| 39 | + } else { |
| 40 | + continue // skip lines without "data: " |
| 41 | + } |
| 42 | + |
| 43 | + // Parse the JSON line into our `Data` struct |
| 44 | + var data Data |
| 45 | + err := json.Unmarshal([]byte(line), &data) |
| 46 | + if err != nil { |
| 47 | + // Skip this line if JSON is incomplete or malformed |
| 48 | + // fmt.Fprintf(os.Stderr, "Warning: could not parse JSON: %v\n", err) |
| 49 | + continue |
| 50 | + } |
| 51 | + |
| 52 | + // Extract delta.content and concatenate it |
| 53 | + for _, choice := range data.Choices { |
| 54 | + contentBuilder.WriteString(choice.Delta.Content) |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + // Check for scanner errors |
| 59 | + if err := scanner.Err(); err != nil { |
| 60 | + return fmt.Errorf("error reading file: %w", err) |
| 61 | + } |
| 62 | + |
| 63 | + // Print the final concatenated result |
| 64 | + result := contentBuilder.String() |
| 65 | + fmt.Println(result) |
| 66 | + |
| 67 | + return nil |
| 68 | +} |
0 commit comments