{"id":25864901,"url":"https://github.com/stackloklabs/gorag","last_synced_at":"2025-03-02T01:31:06.176Z","repository":{"id":260421367,"uuid":"865301666","full_name":"StacklokLabs/gorag","owner":"StacklokLabs","description":"GoRag: Go Interface for Augmented LLM retrieval 📜","archived":false,"fork":false,"pushed_at":"2024-11-11T17:12:07.000Z","size":96,"stargazers_count":10,"open_issues_count":2,"forks_count":3,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-02-03T07:51:56.404Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/StacklokLabs.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-09-30T10:04:03.000Z","updated_at":"2025-02-01T06:47:20.000Z","dependencies_parsed_at":"2024-10-31T08:37:55.911Z","dependency_job_id":null,"html_url":"https://github.com/StacklokLabs/gorag","commit_stats":null,"previous_names":["stackloklabs/gorag"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/StacklokLabs%2Fgorag","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/StacklokLabs%2Fgorag/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/StacklokLabs%2Fgorag/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/StacklokLabs%2Fgorag/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/StacklokLabs","download_url":"https://codeload.github.com/StacklokLabs/gorag/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":241447319,"owners_count":19964306,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2025-03-02T01:31:05.520Z","updated_at":"2025-03-02T01:31:06.132Z","avatar_url":"https://github.com/StacklokLabs.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# GoRag: A Go Library for Retrieval-Augmented Generation (RAG) Development with Multi-Vector Database Support 📜\n\n[![Go Report Card](https://goreportcard.com/badge/github.com/stackloklabs/gorag)](https://goreportcard.com/report/github.com/stackloklabs/gorag)\n[![License](https://img.shields.io/github/license/stackloklabs/gorag)](LICENSE)\n\nGoRag provides an intuitive Go interface for developing Retrieval-Augmented Generation (RAG) applications. It supports multiple vector database types, enabling efficient data retrieval for enhanced generation augmentation.\n\nLanguage Model backends including [Ollama](https://ollama.com) and [OpenAI](https://openai.com), along with an embeddings interface for RAG using a local embeddings model (mxbai-embed-large) or a hosted such type (such as text-embedding-ada-002 from OpenAI)\n\n\n## 🌟 Features\n\n- **Interact with Ollama \u0026 OpenAI:** Generate responses from multiple AI backends.\n- **RAG / Embeddings Generation:** Generate text embeddings store / load to a vector database for RAG.\n- **Multiple Vector Database Support:** Currently Postgres with pgvector is supported, along with qdrant (others to follow, open an issue if you want to see something included).\n---\n\n## 🚀 Getting Started\n\n### 1. Installation\n\ngorag needs to be installed as a dependency in your project. You can do it by importing it in your codebase:\n\n```go\nimport \"github.com/stackloklabs/gorag\"\n```\n\nThen make sure that you have Go installed, and run:\n\n```bash\ngo mod tidy\n```\n\n##  2. Setting Up Ollama\n\nYou'll need to have an Ollama server running and accessible.\n\nInstall Ollama Server: Download the server from the [official Ollama website](https://ollama.com/download)\n\nPull and run a model\n\n```bash\nollama run llama3.2\n```\n\nOllama should run on port `11434` and `localhost`, if you change this, don't\nforget to update your config.\n\n## 3. OpenAI\n\nYou'll need an OpenAI API key to use the OpenAI backend.\n\n## 4. Configuration\n\nCurrently Postgres is supported, and the database should be created before\nrunning the application, with the schema provided in `db/init.sql`\n\nShould you prefer, the docker-compose will automate the setup of the database.\n\n# 🛠️ Usage\n\nBest bet is to see `/examples/*` for reference, this explains how to use\nthe library with examples for generation, embeddings and implementing RAG for pgvector or qdrant.\n\nThere are currently two backend systems supported, Ollama and OpenAI, with\nthe ability to generate embeddings for RAG on both.\n\n## Ollama\n\nFirst create a Backend object\n\n```go\ngenerationBackend := backend.NewOllamaBackend(\"http://localhost:11434\", \"llama3\", time.Duration(10*time.Second))\n```\n\nCreate a prompt\n\n```go\nprompt := backend.NewPrompt().\n\t\tAddMessage(\"system\", \"You are an AI assistant. Use the provided context to answer the user's question as accurately as possible.\").\n\t\tAddMessage(\"user\", \"What is love?\").\n\t\tSetParameters(backend.Parameters{\n\t\t\tMaxTokens:        150,\n\t\t\tTemperature:      0.7,\n\t\t\tTopP:             0.9,\n\t\t})\n```\n\nCall the Generations API\n\n```go\nresponse, err := generationBackend.Generate(ctx, prompt)\nif err != nil {\n    log.Fatalf(\"Failed to generate response: %v\", err)\n}\n```\n\n## OpenAI\n\nFirst create a Backend object\n\n```go\ngenerationBackend = backend.NewOpenAIBackend(\"API_KEY\", \"gpt-3.5-turbo\", 10*time.Second)\n```\n\nCreate a prompt\n\n```go\nprompt := backend.NewPrompt().\n    AddMessage(\"system\", \"You are an AI assistant. Use the provided context to answer the user's question as accurately as possible.\").\n    AddMessage(\"user\", \"How much is too much?\").\n    SetParameters(backend.Parameters{\n        MaxTokens:        150,\n        Temperature:      0.7,\n        TopP:             0.9,\n        FrequencyPenalty: 0.5,\n        PresencePenalty:  0.6,\n    })\n```\n\nCall the Generations API and get a response\n\n```go\nresponse, err := generationBackend.Generate(ctx, prompt)\nif err != nil {\n    log.Fatalf(\"Failed to generate response: %v\", err)\n}\n```\n\n## RAG\n\nTo generate embeddings for RAG, you can use the `Embeddings` interface in both\nOllama and OpenAI backends.\n\n```go\nembedding, err := embeddingBackend.Embed(ctx, \"Mickey mouse is a real human being\")\nif err != nil {\n    log.Fatalf(\"Error generating embedding: %v\", err)\n}\nlog.Println(\"Embedding generated\")\n\n// Embed the query using the specified embedding backend\nqueryEmbedding, err := embeddingBackend.Embed(ctx, query)\nif err != nil {\n    log.Fatalf(\"Error generating query embedding: %v\", err)\n}\nlog.Println(\"Vector embeddings generated\")\n\n// Retrieve relevant documents for the query embedding\nretrievedDocs, err := vectorDB.QueryRelevantDocuments(ctx, queryEmbedding, \"ollama\")\nif err != nil {\n    log.Fatalf(\"Error retrieving relevant documents: %v\", err)\n}\n\n// Log the retrieved documents to see if they include the inserted content\nfor _, doc := range retrievedDocs {\n    log.Printf(\"Retrieved Document: %v\", doc)\n}\n\n// Augment the query with retrieved context\naugmentedQuery := db.CombineQueryWithContext(query, retrievedDocs)\n\nprompt := backend.NewPrompt().\n    AddMessage(\"system\", \"You are an AI assistant. Use the provided context to answer the user's question as accurately as possible.\").\n    AddMessage(\"user\", augmentedQuery).\n    SetParameters(backend.Parameters{\n        MaxTokens:   150, // Supported by LLaMa\n        Temperature: 0.7, // Supported by LLaMa\n        TopP:        0.9, // Supported by LLaMa\n    })\n```\n\nExample output:\n\n```\n2024/10/28 15:08:25 Embedding backend LLM: mxbai-embed-large\n2024/10/28 15:08:25 Generation backend: llama3\n2024/10/28 15:08:25 Vector database initialized\n2024/10/28 15:08:26 Embedding generated\n2024/10/28 15:08:26 Vector Document generated\n2024/10/28 15:08:26 Vector embeddings generated\n2024/10/28 15:08:26 Retrieved Document: {doc-5630d3f2-bf61-4e13-8ec9-9e863bc1a962 map[content:Mickey mouse is a real human being]}\n2024/10/28 15:08:34 Retrieval-Augmented Generation influenced output from LLM model: Mickey Mouse is indeed a human!\n```\n\n# 📝 Contributing\n\nWe welcome contributions! Please submit a pull request or raise an issue if\nyou want to see something included or hit a bug.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstackloklabs%2Fgorag","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fstackloklabs%2Fgorag","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstackloklabs%2Fgorag/lists"}