Skip to content

Commit

Permalink
Merge pull request #11 from richardsonlima/develop
Browse files Browse the repository at this point in the history
adding setup
  • Loading branch information
richardsonlima authored Sep 24, 2024
2 parents f57e5c2 + 1e6bd8e commit f9de349
Show file tree
Hide file tree
Showing 7 changed files with 485 additions and 423 deletions.
3 changes: 2 additions & 1 deletion __init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from .context_manager import ContextManager
from .prompt_builder import PromptBuilder
from .context_optimizer import ContextOptimizer

__all__ = ["ContextManager", "PromptBuilder"]
__all__ = ["ContextManager", "PromptBuilder", "ContextOptimizer"]
68 changes: 68 additions & 0 deletions __main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import os
import openai
from pyicl import ContextManager, PromptBuilder, ContextOptimizer
import nltk
nltk.download('stopwords')
from nltk.tokenize import word_tokenize
from nltk.corpus import stopwords
from nltk.stem import WordNetLemmatizer

def main():
# Load API key from environment variable
api_key = os.environ.get("OPENAI_API_KEY")
client = openai.Client(api_key=api_key)

# Create a ContextManager instance
context_manager = ContextManager()

# Create a PromptBuilder instance with the ContextManager
prompt_builder = PromptBuilder(context_manager)

# Create a ContextOptimizer instance
context_optimizer = ContextOptimizer()

# 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."
])

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

# 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)

# 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)

# Print the built prompt
print(prompt)

# Call OpenAI API
model = "gpt-3.5-turbo"
max_tokens = 2048
temperature = 0.7
response = call_openai_api(model, prompt, max_tokens, temperature)

# Print the model's response
if response is not None:
print("Model Output:")
print(response)
else:
print("Failed to retrieve model response.")

if __name__ == "__main__":
main()
162 changes: 162 additions & 0 deletions context_manager.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
class ContextManager:
"""
A class used to manage contexts and their corresponding examples.
Attributes:
----------
contexts : dict
A dictionary where the keys are context names and the values are lists of examples.
Methods:
-------
add_context(context_name, examples)
Adds a new list of examples for a specific context.
remove_context(context_name)
Removes a context by name.
get_context(context_name)
Retrieves the examples of a specific context.
list_contexts()
Lists all available contexts.
search_context(query)
Searches for contexts that match a query.
filter_contexts(filter_func)
Filters contexts using a custom function.
merge_contexts(other)
Merges two context managers into one.
"""

def __init__(self):
"""
Initializes an empty context manager.
Returns:
-------
None
"""
self.contexts = {}

def add_context(self, context_name: str, examples: list):
"""
Adds a new list of examples for a specific context.
Args:
----
context_name (str): The name of the context.
examples (list): A list of examples for the context.
Returns:
-------
None
"""
if context_name in self.contexts:
self.contexts[context_name].extend(examples)
else:
self.contexts[context_name] = examples

def remove_context(self, context_name: str):
"""
Removes a context by name.
Args:
----
context_name (str): The name of the context to remove.
Returns:
-------
None
"""
self.contexts.pop(context_name, None)

def get_context(self, context_name: str) -> list:
"""
Retrieves the examples of a specific context.
Args:
----
context_name (str): The name of the context.
Returns:
-------
list: A list of examples for the context.
"""
return self.contexts.get(context_name, [])

def list_contexts(self) -> list:
"""
Lists all available contexts.
Returns:
-------
list: A list of context names.
"""
return list(self.contexts.keys())

def search_context(self, query: str) -> list:
"""
Searches for contexts that match a query.
Args:
----
query (str): The query to search for.
Returns:
-------
list: A list of tuples containing the context name and example.
"""
results = []
for context_name, examples in self.contexts.items():
for example in examples:
if query in example:
results.append((context_name, example))
return results

def filter_contexts(self, filter_func: callable) -> dict:
"""
Filters contexts using a custom function.
Args:
----
filter_func (callable): A function that takes an example and returns a boolean.
Returns:
-------
dict: A dictionary of filtered contexts.
"""
filtered_contexts = {}
for context_name, examples in self.contexts.items():
filtered_examples = [example for example in examples if filter_func(example)]
if filtered_examples:
filtered_contexts[context_name] = filtered_examples
return filtered_contexts

def merge_contexts(self, other: 'ContextManager') -> 'ContextManager':
"""
Merges two context managers into one.
Args:
----
other (ContextManager): The context manager to merge with.
Returns:
-------
ContextManager: A new context manager containing the merged contexts.
"""
merged_contexts = {}
for context_name, examples in self.contexts.items():
merged_contexts[context_name] = examples
for context_name, examples in other.contexts.items():
if context_name in merged_contexts:
merged_contexts[context_name].extend(examples)
else:
merged_contexts[context_name] = examples
return ContextManager(**merged_contexts)

def __str__(self) -> str:
"""
Returns a string representation of the context manager.
Returns:
-------
str: A string representation of the context manager.
"""
return f"ContextManager with {len(self.contexts)} contexts"
95 changes: 95 additions & 0 deletions context_optimizer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
class ContextOptimizer:
"""
A class used to optimize the list of contextual examples based on their performance.
Attributes:
----------
example_performance : dict
A dictionary storing the performance metrics for each example.
contexts : list
A list of contextual examples.
Methods:
-------
evaluate_relevance(example, query, performance_metric)
Evaluates the relevance of an example and updates its performance metrics.
adjust_contexts()
Refines the list of examples based on their performance metrics.
experiment(new_context)
Allows users to experiment with different contexts and measures their impact.
get_optimized_context()
Returns the optimized list of contextual examples.
"""

def __init__(self):
"""
Initializes an empty context optimizer.
Returns:
-------
None
"""
self.example_performance = {} # store performance metrics for each example
self.contexts = [] # store the list of contextual examples

def evaluate_relevance(self, example: str, query: str, performance_metric: float):
"""
Evaluates the relevance of an example and updates its performance metrics.
Args:
----
example (str): The example to evaluate.
query (str): The query related to the example.
performance_metric (float): The performance metric of the example.
Returns:
-------
None
"""
# assess the effectiveness of the example in improving model performance
# update the performance metrics for the example
self.example_performance[example] = performance_metric

def adjust_contexts(self):
"""
Refines the list of examples based on their performance metrics.
Returns:
-------
None
"""
# automatically refine the list of examples based on feedback and performance metrics
sorted_examples = sorted(self.example_performance, key=self.example_performance.get, reverse=True)
self.contexts = sorted_examples[:10] # keep only the top 10 performing examples

def experiment(self, new_context: list):
"""
Allows users to experiment with different contexts and measures their impact.
Args:
----
new_context (list): The new context to experiment with.
Returns:
-------
None
"""
# allow users to experiment with different contexts and measure their impact
# temporarily replace the current context with the new one
original_context = self.contexts
self.contexts = new_context
# run the model with the new context and measure the performance
# ...
# restore the original context
self.contexts = original_context

def get_optimized_context(self) -> list:
"""
Returns the optimized list of contextual examples.
Returns:
-------
list: The optimized list of contextual examples.
"""
# return the optimized list of contextual examples
return self.contexts
Loading

0 comments on commit f9de349

Please sign in to comment.