Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

81 wiersze
3.8KB

  1. # Infrastructure layer: configuration, PostgreSQL access (psql inside the container) and the
  2. # embedding provider (Ollama). Nothing here is exported; agents use the Public commands.
  3. function Get-FkeConfig {
  4. # Read at call time so environment overrides (and the test suite's scratch database) take effect.
  5. $ollama = if ($env:FOUNDRY_OLLAMA_URL) { $env:FOUNDRY_OLLAMA_URL } else { 'http://localhost:11434' }
  6. [pscustomobject]@{
  7. Container = if ($env:FOUNDRY_PG_CONTAINER) { $env:FOUNDRY_PG_CONTAINER } else { 'foundry-postgres' }
  8. User = if ($env:FOUNDRY_PG_USER) { $env:FOUNDRY_PG_USER } else { 'foundry' }
  9. Database = if ($env:FOUNDRY_PG_DATABASE) { $env:FOUNDRY_PG_DATABASE } else { 'foundry' }
  10. OllamaUrl = $ollama.TrimEnd('/')
  11. EmbeddingModel = if ($env:FOUNDRY_EMBEDDING_MODEL) { $env:FOUNDRY_EMBEDDING_MODEL } else { 'nomic-embed-text' }
  12. EmbeddingDimension = 768 # must match VECTOR(768) in migrations/001-initial-schema.sql
  13. }
  14. }
  15. function Invoke-FkeSql {
  16. <#
  17. Runs SQL through psql inside the container, on stdin. Caller-supplied values never get
  18. concatenated into SQL: they travel as one JSON payload inside a randomly tagged dollar-quoted
  19. literal, read back in SQL with current_setting('fke.p')::jsonb.
  20. Returns the parsed JSON of the single output value (queries should SELECT one jsonb value),
  21. or the raw text with -Text.
  22. #>
  23. [CmdletBinding()]
  24. param(
  25. [Parameter(Mandatory)][string]$Sql,
  26. $Payload,
  27. [string]$Database,
  28. [switch]$Text
  29. )
  30. $cfg = Get-FkeConfig
  31. if (-not $Database) { $Database = $cfg.Database }
  32. $header = ''
  33. if ($null -ne $Payload) {
  34. $json = $Payload | ConvertTo-Json -Depth 20 -Compress
  35. do { $tag = 'fke' + [Guid]::NewGuid().ToString('N') } while ($json.Contains($tag))
  36. $q = '$' + $tag + '$'
  37. $header = "SELECT set_config('fke.p', ${q}${json}${q}, false) AS ok \gset`n"
  38. }
  39. $script = $header + $Sql + "`n"
  40. $savedConsole = [Console]::OutputEncoding
  41. $savedOut = $OutputEncoding
  42. [Console]::OutputEncoding = [Text.UTF8Encoding]::new($false)
  43. $OutputEncoding = [Text.UTF8Encoding]::new($false)
  44. try {
  45. $out = $script | & docker exec -i $cfg.Container psql -U $cfg.User -d $Database -X -q -t -A -v ON_ERROR_STOP=1 2>&1
  46. $code = $LASTEXITCODE
  47. } finally {
  48. [Console]::OutputEncoding = $savedConsole
  49. $OutputEncoding = $savedOut
  50. }
  51. $lines = @($out | ForEach-Object { "$_" })
  52. if ($code -ne 0) { throw "Knowledge database error (exit $code): $($lines -join ' ')" }
  53. $joined = ($lines -join "`n").Trim()
  54. if ($Text) { return $joined }
  55. if (-not $joined) { return $null }
  56. $joined | ConvertFrom-Json -Depth 50
  57. }
  58. function Get-FkeEmbedding {
  59. # Returns [double[]] with the configured dimension, or throws. Callers decide how to degrade.
  60. param([Parameter(Mandatory)][string]$Text)
  61. $cfg = Get-FkeConfig
  62. $body = @{ model = $cfg.EmbeddingModel; input = $Text } | ConvertTo-Json -Compress
  63. $r = Invoke-RestMethod -Uri "$($cfg.OllamaUrl)/api/embed" -Method Post -ContentType 'application/json; charset=utf-8' `
  64. -Body ([Text.Encoding]::UTF8.GetBytes($body)) -TimeoutSec 60
  65. if (-not $r.embeddings -or -not $r.embeddings[0]) { throw "Ollama returned no embedding for model '$($cfg.EmbeddingModel)'." }
  66. $vec = [double[]]@($r.embeddings[0])
  67. if ($vec.Count -ne $cfg.EmbeddingDimension) {
  68. throw "Embedding model '$($cfg.EmbeddingModel)' returned $($vec.Count) dimensions; the schema stores $($cfg.EmbeddingDimension)."
  69. }
  70. , $vec
  71. }
  72. function Get-FkeTokenEstimate {
  73. # Rough heuristic (about 4 characters per token); not a tokenizer. Used only for budgeting.
  74. param([Parameter(Mandatory)][string]$Text)
  75. [int][math]::Ceiling($Text.Length / 4.0)
  76. }

Powered by TurnKey Linux.