grammar_test.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. package llama
  2. import (
  3. "bufio"
  4. "bytes"
  5. "strings"
  6. "testing"
  7. )
  8. // https://github.com/ollama/ollama/issues/7978
  9. const issue7978JSONSchema = `{
  10. "type": "object",
  11. "properties": {
  12. "steps": {
  13. "type": "array",
  14. "items": {
  15. "type": "object",
  16. "properties": {
  17. "explanation": { "type": "string" },
  18. "output": { "type": "string" }
  19. },
  20. "required": ["explanation", "output"],
  21. "additionalProperties": false
  22. }
  23. },
  24. "final_answer": { "type": "string" }
  25. },
  26. "required": ["steps", "final_answer"],
  27. "additionalProperties": false
  28. }`
  29. func TestIssue7978(t *testing.T) {
  30. t.Skip("schema_to_grammar is broken; skipping until fixed")
  31. g := SchemaToGrammar([]byte(issue7978JSONSchema))
  32. if g == nil {
  33. t.Fatal("failed to convert JSON schema to grammar")
  34. }
  35. t.Logf("grammar:\n%s", g)
  36. t.Log()
  37. var sawSteps bool
  38. s := bufio.NewScanner(bytes.NewReader(g))
  39. for s.Scan() {
  40. line := s.Text()
  41. if strings.Contains(line, "steps") {
  42. sawSteps = true
  43. }
  44. if strings.Contains(line, "final-answer") && !sawSteps {
  45. t.Error("expected 'steps' before 'final-answer'")
  46. }
  47. }
  48. }
  49. func TestSchemaToGrammer(t *testing.T) {
  50. t.Skip("schema_to_grammar is broken; skipping until fixed")
  51. cases := []struct {
  52. schema string
  53. prefix []byte // nil is check as nil
  54. }{
  55. {`invalid`, nil},
  56. // Simple heuristic/smoke test
  57. {`{"type":"object"}`, []byte("object ::=")},
  58. }
  59. for _, c := range cases {
  60. t.Run("x", func(t *testing.T) {
  61. g := SchemaToGrammar([]byte(c.schema))
  62. if c.prefix == nil && g != nil {
  63. t.Fatalf("grammar = %v, want nil", g)
  64. }
  65. if !bytes.HasPrefix(g, c.prefix) {
  66. t.Errorf("grammar = %q, want %q", g, c.prefix)
  67. }
  68. })
  69. }
  70. }