Skip to content

Commit

Permalink
Merge pull request #19 from richardsonlima/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
richardsonlima authored Oct 23, 2024
2 parents 6e0b7da + 116ae60 commit e35d692
Show file tree
Hide file tree
Showing 16 changed files with 690 additions and 139 deletions.
537 changes: 537 additions & 0 deletions PaperList.md

Large diffs are not rendered by default.

13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,24 @@
**SynapSense** Python In-Context Learning for Large Language Models
SynapSense is a cutting-edge Python library designed to streamline the implementation of In-Context Learning (ICL) with Large Language Models (LLMs). By combining the concept of "synapse" (neural connections) with "sense," SynapSense empowers developers to build intelligent, sense-making models that leverage contextual information for more accurate and dynamic learning. Whether you’re working with natural language processing, AI-driven applications, or advanced machine learning projects, SynapSense provides an intuitive, scalable framework for enhancing LLM performance with in-context capabilities.

## Python Package
pypi.org/project/synapsense

## Demo
![Alt text](ttyrecord.gif)
![Alt text](media/ttyrecord.gif)

In-Context Learning refers to the technique where a model is provided with examples within the context of its input, allowing the model to learn from these examples without the need for explicit fine-tuning. The idea is to leverage contextual examples to influence the model's output dynamically.

The synapsense library offers tools to manage and utilize these contextual examples efficiently, providing a structured way to build prompts and optimize the context provided to the LLM. synapsense is modular, allowing developers to pick and choose components based on their needs.

## Examples
[OpenAI In-Context Learning (ICL) Streamlit Chat](https://github.com/richardsonlima/synapsense/tree/main/examples/streamlit_example_openai_context_integration.py)

[OpenAI In-Context Learning (ICL)](https://github.com/richardsonlima/synapsense/tree/main/examples/example_openai_context_integration.py)


![Alt text](media/streamlit_example_openai_context_integration.png)

## Usage
This component is particularly useful in scenarios where the diversity of examples is critical, such as in adversarial settings or when working with limited data.

Expand Down
49 changes: 36 additions & 13 deletions examples/example_openai_context_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def call_openai_api(model: str, prompt: str, max_tokens: int, temperature: float
try:
response = client.chat.completions.create(model=model,
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "system", "content": "You are an expert assistant specialized in Retrieval-Augmented Generation (RAG), In-Context Learning (ICL), and Retrieval-Based Context. Your role is to provide clear, accurate, and detailed explanations of these concepts based on recent academic research. You should assist users by clarifying the differences, applications, and benefits of these methods in large language models (LLMs). When answering, ensure to refer to the latest findings on these techniques, offering examples and insights from research papers to support your explanations."},
{"role": "user", "content": prompt}
],
max_tokens=max_tokens,
Expand All @@ -68,32 +68,55 @@ def main() -> None:
# Create a ContextOptimizer instance
context_optimizer = create_context_optimizer()

# Add some examples for the "medical" context
context_manager.add_context("medical", [
"A patient is experiencing symptoms of fever and headache.",
"A patient has been diagnosed with diabetes and needs treatment.",
"A patient is experiencing symptoms of chest pain and shortness of breath.",
"A patient has been diagnosed with hypertension and needs medication.",
"A patient is experiencing symptoms of dizziness and nausea."
# Add some examples for the "AI-Research" context
context_manager.add_context("AI-Research", [
"Retrieval-Augmented Generation (RAG) enhances language models by integrating external information retrieval with generation capabilities.",
"It retrieves relevant knowledge based on the user's query and then generates a response grounded in this context.",
"The goal of RAG is to improve accuracy and relevance by incorporating external knowledge, making it ideal for tasks like question answering and knowledge grounding.",
"Retrieval-Augmented Generation (RAG) verbessert Sprachmodelle, indem es externe Informationsabrufe mit Generierungsfunktionen kombiniert.",
"Es ruft relevantes Wissen basierend auf der Benutzeranfrage ab und generiert dann eine Antwort, die in diesem Kontext verankert ist.",
"Das Ziel von RAG ist es, die Genauigkeit und Relevanz zu verbessern, indem externes Wissen einbezogen wird, was es ideal für Aufgaben wie die Beantwortung von Fragen und das Fundieren von Wissen macht.",
"A Geração Aumentada por Recuperação (RAG) melhora os modelos de linguagem ao integrar a recuperação de informações externas com capacidades de geração.",
"Ela recupera conhecimento relevante com base na consulta do usuário e gera uma resposta fundamentada nesse contexto.",
"O objetivo do RAG é melhorar a precisão e a relevância, incorporando conhecimento externo, tornando-o ideal para tarefas como respostas a perguntas e fundamentação de conhecimento.",
"In-Context Learning (ICL) allows language models to adapt to new tasks using examples provided in the input context without retraining.",
"ICL leverages patterns from the input examples to generate predictions or responses based on the context, making it suitable for a wide range of tasks.",
"The objective of ICL is to enable flexible learning without explicit fine-tuning, allowing models to perform well even in zero-shot or few-shot scenarios.",
"In-Context Learning (ICL) ermöglicht es Sprachmodellen, sich anhand von im Eingabekontext bereitgestellten Beispielen an neue Aufgaben anzupassen, ohne dass ein erneutes Training erforderlich ist.",
"ICL nutzt Muster aus den Eingabebeispielen, um Vorhersagen oder Antworten auf Basis des Kontexts zu generieren, was es für eine Vielzahl von Aufgaben geeignet macht.",
"Das Ziel von ICL ist es, flexibles Lernen ohne explizites Feintuning zu ermöglichen und Modelle auch in Zero-Shot- oder Few-Shot-Szenarien erfolgreich zu machen.",
"O Aprendizado no Contexto (ICL) permite que modelos de linguagem se adaptem a novas tarefas usando exemplos fornecidos no contexto de entrada, sem necessidade de retreinamento.",
"ICL aproveita padrões dos exemplos de entrada para gerar previsões ou respostas com base no contexto, tornando-o adequado para uma ampla gama de tarefas.",
"O objetivo do ICL é possibilitar o aprendizado flexível sem ajuste fino explícito, permitindo que os modelos tenham bom desempenho mesmo em cenários de zero-shot ou few-shot.",
"Retrieval-Based Context involves querying an external knowledge base to retrieve relevant information that informs the model's response.",
"This method is highly effective in ensuring accuracy, especially in tasks that require real-time or up-to-date knowledge.",
"The primary difference from ICL is that Retrieval-Based Context dynamically accesses external data, while ICL works purely from internal examples.",
"Der Retrieval-Based Context umfasst das Abfragen einer externen Wissensdatenbank, um relevante Informationen abzurufen, die die Antwort des Modells beeinflussen.",
"Diese Methode ist besonders effektiv, um Genauigkeit zu gewährleisten, insbesondere bei Aufgaben, die Echtzeit- oder aktuelle Kenntnisse erfordern.",
"Der Hauptunterschied zu ICL besteht darin, dass der Retrieval-Based Context dynamisch auf externe Daten zugreift, während ICL rein auf internen Beispielen basiert.",
"O Contexto Baseado em Recuperação envolve a consulta a uma base de conhecimento externa para recuperar informações relevantes que informam a resposta do modelo.",
"Esse método é altamente eficaz em garantir precisão, especialmente em tarefas que exigem conhecimento em tempo real ou atualizado.",
"A principal diferença do ICL é que o Contexto Baseado em Recuperação acessa dinamicamente dados externos, enquanto o ICL trabalha puramente com exemplos internos."

])

# Get the context examples
context_examples = context_manager.get_context("medical")
context_examples = context_manager.get_context("AI-Research")

# Evaluate the relevance of each context example
for example in context_examples:
# For demonstration purposes, assume a performance metric of 0.5
context_optimizer.evaluate_relevance(example, "medical", 0.5)
context_optimizer.evaluate_relevance(example, "AI-Research", 0.5)

# Adjust the context examples based on their relevance
context_optimizer.adjust_contexts()

# Get the optimized context examples
optimized_context = context_optimizer.get_optimized_context()

# Build a prompt for the "medical" context with a user input
user_input = "A patient is experiencing symptoms of chest pain and shortness of breath."
prompt = prompt_builder.build_prompt("medical", user_input)
# Build a prompt for the "AI-Research" context with a user input
user_input = "Explain if In-Context Learning (ICL) with Retrieval-Augmented Generation (RAG) improves the accuracy of language models in providing relevant responses, particularly in real-time applications."
prompt = prompt_builder.build_prompt("AI-Research", user_input)
# Print the built prompt
print(prompt)

Expand Down
123 changes: 0 additions & 123 deletions examples/example_openai_integration.py

This file was deleted.

Loading

0 comments on commit e35d692

Please sign in to comment.