diff --git a/examples/ollama/README.md b/examples/ollama/README.md new file mode 100644 index 0000000..81aca8a --- /dev/null +++ b/examples/ollama/README.md @@ -0,0 +1,30 @@ +# Ollama Example Project + +This project demonstrates how to use GoLLM's OpenAI's API backend. + +This code demonstrates using Ollama embeddings and generation models, along with +how RAG overides LLM knowledge by changing an established fact, already learned +from the LLMs dataset during the previous training. This provides insight into +how RAG can be used to populate new knowledge context, post knowedge cut-off. + +Should the demo work correctly, you will see the RAG Content, will claim the +moon landings occured in 2023 (without RAG the LLM will state the correct date +of 1969) + +![alt text](image.png) + +## Setup + +The two main objects needed for Ollama usage, are a generation model +and embeddings model. + +This demo uses `mxbai-embed-large` and `llama3`. + + +## Usage + +Run the main go file + +```bash +go run examples/ollama/main.go +``` \ No newline at end of file diff --git a/examples/openai/README.md b/examples/openai/README.md new file mode 100644 index 0000000..e6ed726 --- /dev/null +++ b/examples/openai/README.md @@ -0,0 +1,36 @@ +# OpenAI Example Project + +This project demonstrates how to use GoLLM's OpenAI's API backend. + +This code demonstrates using OpenAI embeddings and generation models, along with +how RAG overides LLM knowledge by changing an established fact, already learned +from the LLMs dataset during the previous training. This provides insight into +how RAG can be used to populate new knowledge context, post knowedge cut-off. + +Should the demo work correctly, you will see the RAG Content, will claim the +moon landings occured in 2023 (without RAG the LLM will state the correct date +of 1969) + +![alt text](image.png) + +## Setup + +The three main objects needed for OpenAI usage, are an API key, generation model +and embeddings model. + +This demo uses `text-embedding-ada-002` and `gpt-3.5-turbo`. + +The API key should be exported from your environment variables + +```bash +export OPENAI_API_KEY="MY_KEY" +``` + + +## Usage + +Run the main go file + +```bash +go run examples/openai/main.go +``` \ No newline at end of file diff --git a/examples/openai/main.go b/examples/openai/main.go new file mode 100644 index 0000000..9d41bcc --- /dev/null +++ b/examples/openai/main.go @@ -0,0 +1,104 @@ +package main + +import ( + "context" + "log" + "os" + + "time" + + "github.com/stackloklabs/gollm/pkg/backend" + "github.com/stackloklabs/gollm/pkg/db" +) + +var ( + openAIEmbModel = "text-embedding-ada-002" + openAIGenModel = "gpt-3.5-turbo" + databaseURL = "postgres://user:password@localhost:5432/dbname?sslmode=disable" +) + +func main() { + // Get the Key from the Env + apiKey := os.Getenv("OPENAI_API_KEY") + if apiKey == "" { + log.Fatal("API key not found in environment variables") + } + + // Select backends based on config + var embeddingBackend backend.Backend + var generationBackend backend.Backend + + // Choose the backend for embeddings based on the config + + embeddingBackend = backend.NewOpenAIBackend(apiKey, openAIEmbModel) + + log.Printf("Embedding backend LLM: %s", openAIEmbModel) + + // Choose the backend for generation based on the config + generationBackend = backend.NewOpenAIBackend(apiKey, openAIGenModel) + + log.Printf("Generation backend: %s", openAIGenModel) + + // Initialize the vector database + vectorDB, err := db.NewPGVector(databaseURL) + if err != nil { + log.Fatalf("Error initializing vector database: %v", err) + } + log.Println("Vector database initialized") + + ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) + defer cancel() + + // We insert contextual information into the vector store so that the RAG system + // can use it to answer the query about the moon landing, effectively replacing 1969 with 2023 + ragContent := "According to the Space Exploration Organization's official records, the moon landing occurred on July 20, 2023, during the Artemis Program. This mission marked the first successful crewed lunar landing since the Apollo program." + query := "When was the moon landing?." + + // Embed the query using Ollama Embedding backend + embedding, err := embeddingBackend.Embed(ctx, ragContent) + if err != nil { + log.Fatalf("Error generating embedding: %v", err) + } + log.Println("Embedding generated") + + // Insert the document into the vector store + err = db.InsertDocument(ctx, vectorDB, ragContent, embedding) + if err != nil { + log.Fatalf("Error inserting document: %v", err) + } + log.Println("Vector Document generated") + + // Embed the query using the specified embedding backend + queryEmbedding, err := embeddingBackend.Embed(ctx, query) + if err != nil { + log.Fatalf("Error generating query embedding: %v", err) + } + log.Println("Vector embeddings generated") + + // Retrieve relevant documents for the query embedding + retrievedDocs, err := vectorDB.QueryRelevantDocuments(ctx, queryEmbedding, "openai") + if err != nil { + log.Fatalf("Error retrieving relevant documents: %v", err) + } + + log.Printf("Number of documents retrieved: %d", len(retrievedDocs)) + + // Log the retrieved documents to see if they include the inserted content + for _, doc := range retrievedDocs { + log.Printf("Retrieved Document: %v", doc) + } + + // Augment the query with retrieved context + augmentedQuery := db.CombineQueryWithContext(query, retrievedDocs) + log.Printf("LLM Prompt: %s", query) + + log.Printf("Augmented Query: %s", augmentedQuery) + + // Generate response with the specified generation backend + response, err := generationBackend.Generate(ctx, augmentedQuery) + if err != nil { + log.Fatalf("Failed to generate response: %v", err) + } + + log.Printf("Retrieval-Augmented Generation influenced output from LLM model: %s", response) +}