structured_outputs.go 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. package sample
  2. import "github.com/ollama/ollama/model"
  3. type StructuredOutput struct {
  4. schema *Schema
  5. }
  6. func BuildStructuredOutputGraph(schema *Schema, proc model.TextProcessor) *PDANode {
  7. // _, stateToNodeMap, err := BuildGraph(proc)
  8. // if err != nil {
  9. // panic(err)
  10. // }
  11. return nil
  12. }
  13. // func constrainGraph(graph *PDANode, schema *Schema) *PDANode {
  14. // // If no schema constraints, return original graph node
  15. // if schema == nil {
  16. // return graph
  17. // }
  18. // // Create a new node with same state
  19. // constrainedNode := NewPDANode(graph.State)
  20. // // Copy over existing transitions and masks
  21. // constrainedNode.TransitionEdges = make(map[rune]*PDANode)
  22. // for r, node := range graph.TransitionEdges {
  23. // constrainedNode.TransitionEdges[r] = node
  24. // }
  25. // constrainedNode.MaskTokenIDToNode = graph.MaskTokenIDToNode
  26. // // Apply schema constraints based on type
  27. // switch schema.EffectiveType() {
  28. // case "object":
  29. // // Only allow defined property names in object keys
  30. // if graph.State == StateInObjectKey {
  31. // // TODO: Add property name validation
  32. // }
  33. // // Constrain property values based on schema
  34. // if graph.State == StateInColon || graph.State == StateInSpace {
  35. // // Clear transitions to only allow valid types
  36. // constrainedNode.TransitionEdges = make(map[rune]*PDANode)
  37. // // Add transitions based on property schemas
  38. // for _, prop := range schema.Properties {
  39. // switch prop.EffectiveType() {
  40. // case "object":
  41. // if objNode, ok := graph.TransitionEdges['{']; ok {
  42. // constrainedNode.TransitionEdges['{'] = constrainGraph(objNode, prop)
  43. // }
  44. // case "array":
  45. // if arrNode, ok := graph.TransitionEdges['[']; ok {
  46. // constrainedNode.TransitionEdges['['] = constrainGraph(arrNode, prop)
  47. // }
  48. // case "string":
  49. // if strNode, ok := graph.TransitionEdges['"']; ok {
  50. // constrainedNode.TransitionEdges['"'] = constrainGraph(strNode, prop)
  51. // }
  52. // case "number":
  53. // for _, r := range validNumberRunes {
  54. // if numNode, ok := graph.TransitionEdges[r]; ok {
  55. // constrainedNode.TransitionEdges[r] = constrainGraph(numNode, prop)
  56. // }
  57. // }
  58. // case "integer":
  59. // for _, r := range validIntRunes {
  60. // if intNode, ok := graph.TransitionEdges[r]; ok {
  61. // constrainedNode.TransitionEdges[r] = constrainGraph(intNode, prop)
  62. // }
  63. // }
  64. // case "boolean":
  65. // for _, r := range []rune{'t', 'f'} {
  66. // if boolNode, ok := graph.TransitionEdges[r]; ok {
  67. // constrainedNode.TransitionEdges[r] = constrainGraph(boolNode, prop)
  68. // }
  69. // }
  70. // case "null":
  71. // if nullNode, ok := graph.TransitionEdges['n']; ok {
  72. // constrainedNode.TransitionEdges['n'] = constrainGraph(nullNode, prop)
  73. // }
  74. // }
  75. // }
  76. // }
  77. // case "array":
  78. // // Constrain array items based on schema
  79. // if schema.Items != nil {
  80. // for r, node := range graph.TransitionEdges {
  81. // constrainedNode.TransitionEdges[r] = constrainGraph(node, schema.Items)
  82. // }
  83. // }
  84. // }
  85. // return constrainedNode
  86. // }