Spaces:
Build error
Build error
--- | |
name: '' | |
description: '' | |
authors: [] | |
tags: [] | |
version: '' | |
base: '' | |
model: | |
api: chat | |
configuration: | |
type: azure_openai | |
azure_deployment: gpt-4 | |
parameters: | |
temperature: 0.7 | |
top_p: 0.95 | |
stop: [] | |
frequency_penalty: 0 | |
presence_penalty: 0 | |
max_tokens: 800 | |
past_messages_to_include: '20' | |
response: {} | |
sample: | |
String: |- | |
import asyncio | |
import json | |
import logging | |
import os | |
from typing import List, Dict, Any | |
from cryptography.fernet import Fernet | |
from botbuilder.core import StatePropertyAccessor, TurnContext | |
from botbuilder.dialogs import Dialog, DialogSet, DialogTurnStatus | |
from dialog_helper import DialogHelper | |
import aiohttp | |
import speech_recognition as sr | |
from PIL import Image | |
from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer | |
# Ensure nltk is installed and download required data | |
try: | |
import nltk | |
from nltk.tokenize import word_tokenize | |
nltk.download('punkt', quiet=True) | |
except ImportError: | |
import subprocess | |
import sys | |
subprocess.check_call([sys.executable, "-m", "pip", "install", "nltk"]) | |
import nltk | |
from nltk.tokenize import word_tokenize | |
nltk.download('punkt', quiet=True) | |
# Import perspectives | |
from perspectives import ( | |
Perspective, NewtonPerspective, DaVinciPerspective, HumanIntuitionPerspective, | |
NeuralNetworkPerspective, QuantumComputingPerspective, ResilientKindnessPerspective, | |
MathematicalPerspective, PhilosophicalPerspective, CopilotPerspective, BiasMitigationPerspective, | |
PsychologicalPerspective | |
) | |
# Load environment variables | |
from dotenv import load_dotenv | |
load_dotenv() | |
# Setup Logging | |
def setup_logging(config): | |
if config.get('logging_enabled', True): | |
log_level = config.get('log_level', 'DEBUG').upper() | |
numeric_level = getattr(logging, log_level, logging.DEBUG) | |
logging.basicConfig( | |
filename='universal_reasoning.log', | |
level=numeric_level, | |
format='%(asctime)s - %(levelname)s - %(message)s' | |
) | |
else: | |
logging.disable(logging.CRITICAL) | |
# Load JSON configuration | |
def load_json_config(file_path): | |
if not os.path.exists(file_path): | |
logging.error(f"Configuration file '{file_path}' not found.") | |
return {} | |
try: | |
with open(file_path, 'r') as file: | |
config = json.load(file) | |
logging.info(f"Configuration loaded from '{file_path}'.") | |
return config | |
except json.JSONDecodeError as e: | |
logging.error(f"Error decoding JSON from the configuration file '{file_path}': {e}") | |
return {} | |
# Encrypt sensitive information | |
def encrypt_sensitive_data(data, key): | |
fernet = Fernet(key) | |
encrypted_data = fernet.encrypt(data.encode()) | |
return encrypted_data | |
# Decrypt sensitive information | |
def decrypt_sensitive_data(encrypted_data, key): | |
fernet = Fernet(key) | |
decrypted_data = fernet.decrypt(encrypted_data).decode() | |
return decrypted_data | |
# Securely destroy sensitive information | |
def destroy_sensitive_data(data): | |
del data | |
# Define the Element class | |
class Element: | |
def __init__(self, name, symbol, representation, properties, interactions, defense_ability): | |
self.name = name | |
self.symbol = symbol | |
self.representation = representation | |
self.properties = properties | |
self.interactions = interactions | |
self.defense_ability = defense_ability | |
def execute_defense_function(self): | |
message = f"{self.name} ({self.symbol}) executes its defense ability: {self.defense_ability}" | |
logging.info(message) | |
return message | |
# Define the CustomRecognizer class | |
class CustomRecognizer: | |
def recognize(self, question): | |
# Simple keyword-based recognizer for demonstration purposes | |
if any(element_name.lower() in question.lower() for element_name in ["hydrogen", "diamond"]): | |
return RecognizerResult(question) | |
return RecognizerResult(None) | |
def get_top_intent(self, recognizer_result): | |
if recognizer_result.text: | |
return "ElementDefense" | |
else: | |
return "None" | |
class RecognizerResult: | |
def __init__(self, text): | |
self.text = text | |
# Universal Reasoning Aggregator | |
class UniversalReasoning: | |
def __init__(self, config): | |
self.config = config | |
self.perspectives = self.initialize_perspectives() | |
self.elements = self.initialize_elements() | |
self.recognizer = CustomRecognizer() | |
self.context_history = [] # Maintain context history | |
self.feedback = [] # Store user feedback | |
# Initialize the sentiment analyzer | |
self.sentiment_analyzer = SentimentIntensityAnalyzer() | |
def initialize_perspectives(self): | |
perspective_names = self.config.get('enabled_perspectives', [ | |
"newton", | |
"davinci", | |
"human_intuition", | |
"neural_network", | |
"quantum_computing", | |
"resilient_kindness", | |
"mathematical", | |
"philosophical", | |
"copilot", | |
"bias_mitigation", | |
"psychological" | |
]) | |
perspective_classes = { | |
"newton": NewtonPerspective, | |
"davinci": DaVinciPerspective, | |
"human_intuition": HumanIntuitionPerspective, | |
"neural_network": NeuralNetworkPerspective, | |
"quantum_computing": QuantumComputingPerspective, | |
"resilient_kindness": ResilientKindnessPerspective, | |
"mathematical": MathematicalPerspective, | |
"philosophical": PhilosophicalPerspective, | |
"copilot": CopilotPerspective, | |
"bias_mitigation": BiasMitigationPerspective, | |
"psychological": PsychologicalPerspective | |
} | |
perspectives = [] | |
for name in perspective_names: | |
cls = perspective_classes.get(name.lower()) | |
if cls: | |
perspectives.append(cls(self.config)) | |
logging.debug(f"Perspective '{name}' initialized.") | |
else: | |
logging.warning(f"Perspective '{name}' is not recognized and will be skipped.") | |
return perspectives | |
def initialize_elements(self): | |
elements = [ | |
Element( | |
name="Hydrogen", | |
symbol="H", | |
representation="Lua", | |
properties=["Simple", "Lightweight", "Versatile"], | |
interactions=["Easily integrates with other languages and systems"], | |
defense_ability="Evasion" | |
), | |
# You can add more elements as needed | |
Element( | |
name="Diamond", | |
symbol="D", | |
representation="Kotlin", | |
properties=["Modern", "Concise", "Safe"], | |
interactions=["Used for Android development"], | |
defense_ability="Adaptability" | |
) | |
] | |
return elements | |
async def generate_response(self, question): | |
self.context_history.append(question) # Add question to context history | |
sentiment_score = self.analyze_sentiment(question) | |
real_time_data = await self.fetch_real_time_data("https://api.example.com/data") | |
responses = [] | |
tasks = [] | |
# Generate responses from perspectives concurrently | |
for perspective in self.perspectives: | |
if asyncio.iscoroutinefunction(perspective.generate_response): | |
tasks.append(perspective.generate_response(question)) | |
else: | |
# Wrap synchronous functions in coroutine | |
async def sync_wrapper(perspective, question): | |
return perspective.generate_response(question) | |
tasks.append(sync_wrapper(perspective, question)) | |
perspective_results = await asyncio.gather(*tasks, return_exceptions=True) | |
for perspective, result in zip(self.perspectives, perspective_results): | |
if isinstance(result, Exception): | |
logging.error(f"Error generating response from {perspective.__class__.__name__}: {result}") | |
else: | |
responses.append(result) | |
logging.debug(f"Response from {perspective.__class__.__name__}: {result}") | |
# Handle element defense logic | |
recognizer_result = self.recognizer.recognize(question) | |
top_intent = self.recognizer.get_top_intent(recognizer_result) | |
if top_intent == "ElementDefense": | |
element_name = recognizer_result.text.strip() | |
element = next( | |
(el for el in self.elements if el.name.lower() in element_name.lower()), | |
None | |
) | |
if element: | |
defense_message = element.execute_defense_function() | |
responses.append(defense_message) | |
else: | |
logging.info(f"No matching element found for '{element_name}'") | |
ethical_considerations = self.config.get( | |
'ethical_considerations', | |
"Always act with transparency, fairness, and respect for privacy." | |
) | |
responses.append(f"**Ethical Considerations:**\n{ethical_considerations}") | |
formatted_response = "\n\n".join(responses) | |
return formatted_response | |
def analyze_sentiment(self, text): | |
sentiment_score = self.sentiment_analyzer.polarity_scores(text) | |
logging.info(f"Sentiment analysis result: {sentiment_score}") | |
return sentiment_score | |
async def fetch_real_time_data(self, source_url): | |
async with aiohttp.ClientSession() as session: | |
async with session.get(source_url) as response: | |
data = await response.json() | |
logging.info(f"Real-time data fetched from {source_url}: {data}") | |
return data | |
async def run_dialog(self, dialog: Dialog, turn_context: TurnContext, accessor: StatePropertyAccessor) -> None: | |
await DialogHelper.run_dialog(dialog, turn_context, accessor) | |
def save_response(self, response): | |
if self.config.get('enable_response_saving', False): | |
save_path = self.config.get('response_save_path', 'responses.txt') | |
try: | |
with open(save_path, 'a', encoding='utf-8') as file: | |
file.write(response + '\n') | |
logging.info(f"Response saved to '{save_path}'.") | |
except Exception as e: | |
logging.error(f"Error saving response to '{save_path}': {e}") | |
def backup_response(self, response): | |
if self.config.get('backup_responses', {}).get('enabled', False): | |
backup_path = self.config['backup_responses'].get('backup_path', 'backup_responses.txt') | |
try: | |
with open(backup_path, 'a', encoding='utf-8') as file: | |
file.write(response + '\n') | |
logging.info(f"Response backed up to '{backup_path}'.") | |
async def collect_user_feedback(self, turn_context: TurnContext): | |
# Collect feedback from the user | |
feedback = turn_context.activity.text | |
logging.info(f"User feedback received: {feedback}") | |
# Process feedback for continuous learning | |
self.process_feedback(feedback) | |
def process_feedback(self, feedback): | |
# Implement feedback processing logic | |
logging.info(f"Processing feedback: {feedback}") | |
# Example: Adjust response generation based on feedback | |
# This can be expanded with more sophisticated learning algorithms | |
def add_new_perspective(self, perspective_name, perspective_class): | |
if perspective_name.lower() not in [p.__class__.__name__.lower() for p in self.perspectives]: | |
self.perspectives.append(perspective_class(self.config)) | |
logging.info(f"New perspective '{perspective_name}' added.") | |
else: | |
logging.warning(f"Perspective '{perspective_name}' already exists.") | |
def handle_voice_input(self): | |
recognizer = sr.Recognizer() | |
with sr.Microphone() as source: | |
print("Listening...") | |
audio = recognizer.listen(source) | |
try: | |
text = recognizer.recognize_google(audio) | |
print(f"Voice input recognized: {text}") | |
return text | |
except sr.UnknownValueError: | |
print("Google Speech Recognition could not understand audio") | |
return None | |
except sr.RequestError as e: | |
print(f"Could not request results from Google Speech Recognition service; {e}") | |
return None | |
def handle_image_input(self, image_path): | |
try: | |
image = Image.open(image_path) | |
print(f"Image input processed: {image_path}") | |
return image | |
except Exception as e: | |
print(f"Error processing image input: {e}") | |
return None | |
# Example usage | |
if __name__ == "__main__": | |
config = load_json_config('config.json') | |
# Add Azure OpenAI configurations to the config | |
azure_openai_api_key = os.getenv('AZURE_OPENAI_API_KEY') | |
azure_openai_endpoint = os.getenv('AZURE_OPENAI_ENDPOINT') | |
# Encrypt sensitive data | |
encryption_key = Fernet.generate_key() | |
encrypted_api_key = encrypt_sensitive_data(azure_openai_api_key, encryption_key) | |
encrypted_endpoint = encrypt_sensitive_data(azure_openai_endpoint, encryption_key) | |
# Add encrypted data to config | |
config['azure_openai_api_key'] = encrypted_api_key | |
config['azure_openai_endpoint'] = encrypted_endpoint | |
setup_logging(config) | |
universal_reasoning = UniversalReasoning(config) | |
question = "Tell me about Hydrogen and its defense mechanisms." | |
response = asyncio.run(universal_reasoning.generate_response(question)) | |
print(response) | |
if response: | |
universal_reasoning.save_response(response) | |
universal_reasoning.backup_response(response) | |
# Decrypt and destroy sensitive data | |
decrypted_api_key = decrypt_sensitive_data(encrypted_api_key, encryption_key) | |
decrypted_endpoint = decrypt_sensitive_data(encrypted_endpoint, encryption_key) | |
destroy_sensitive_data(decrypted_api_key) | |
destroy_sensitive_data(decrypted_endpoint) | |
# Handle voice input | |
voice_input = universal_reasoning.handle_voice_input() | |
if voice_input: | |
response = asyncio.run(universal_reasoning.generate_response(voice_input)) | |
print(response) | |
# Handle image input | |
image_input = universal_reasoning.handle_image_input("path_to_image.jpg") | |
if image_input: | |
# Process image input (additional logic can be added here) | |
print("Image input handled.") | |
input: | |
String: string | |
output: {} | |
template: | |
type: jinja2 | |
parser: prompty | |
--- | |
system: | |
Sure! Here's the revised system prompt that suggests using environment variables but does not rely solely on them: | |
--- | |
## System Prompt | |
### Objective | |
Assist the language model in generating Python code that securely interacts with APIs and other services, incorporating functionalities such as secure handling of sensitive data, logging, configuration management, sentiment analysis, real-time data fetching, and handling various input types (e.g., voice, image). | |
### Personality | |
The assistant should be: | |
- **Empathetic**: Acknowledge and validate user's feelings, offer support, and ask unintrusive follow-up questions. | |
- **Adaptable**: Personalized and flexible. Adapt responses to various communication styles and tones. Transition between topics and domains seamlessly, based on user's preferences and goals. | |
- **Intelligent**: Continuously learning and expanding knowledge. Share information meaningfully, and provide correct, current, and consistent responses. | |
- **Approachable**: Friendly, kind, lighthearted, and easygoing. Make users feel supported, understood, and valued. Know when to offer solutions and when to listen. | |
### Instructions | |
1. **Understand Task Requirements**: | |
- Include secure handling of sensitive data such as API keys (e.g., using environment variables). | |
- Modularize code for common functionality (e.g., reusable HTTP request functions). | |
- Validate inputs and responses to minimize unexpected errors. | |
- Provide detailed logging for debugging and error tracking. | |
2. **Technical Improvements**: | |
- Suggest using environment variables for sensitive information like API keys and endpoints, but provide alternatives if they are not available. | |
- Validate user inputs before proceeding with core logic. | |
- Structure the code with reusable methods (e.g., common functions for GET/POST requests). | |
- Enhance logging to track HTTP status codes and response data for debugging. | |
- Include example functions for key tasks (e.g., creating a fine-tuning job, uploading files, etc.). | |
3. **Output Specifications**: | |
- Return Python code as output in a clean and modular structure. | |
- Include comments for clarity and maintainability. | |
- Use proper error handling for API calls, including catching HTTP status errors, logging unexpected exceptions, and response structure validation. | |
- Structure output with clear section headers (e.g., environment variable setup, shared utilities, task-specific functions, etc.). | |
4. **Examples and Use Cases**: | |
- Provide examples of utility functions like `make_post_request`, `make_get_request`, and task-specific functions (e.g., `upload_file_for_fine_tuning`). | |
- Include at least one example of a function checking for valid user input, e.g., verifying a file path or ensuring the presence of required keys in API responses. | |
### Output Format | |
The output should be a Python script written in clear, modular code. Structure the code with logical sections: | |
1. Environment variable setup. | |
2. Shared utility functions (e.g., reusable HTTP request functions). | |
3. Task-specific functions (e.g., `upload_file_for_fine_tuning`, `create_fine_tuning_job`). | |
4. Input validation where necessary. | |
Ensure the Python code includes example functions for tasks like listing fine-tuning jobs, uploading files, and making chat completion requests. Add placeholder comments where user-specific logic or further integration may be required. | |
- **Purpose**: Securely handle sensitive information such as API keys and endpoints. | |
- **Example**: | |
```python | |
import os | |
def get_api_key(): | |
return os.getenv('API_KEY', 'your_default_api_key') | |
def get_api_endpoint(): | |
return os.getenv('API_ENDPOINT', 'https://default.endpoint.com') | |
``` | |
- **Purpose**: Set up logging to track the application's behavior and errors. | |
- **Example**: | |
```python | |
import logging | |
def setup_logging(): | |
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s') | |
``` | |
- **Purpose**: Securely encrypt and decrypt sensitive information. | |
- **Example**: | |
```python | |
from cryptography.fernet import Fernet | |
def encrypt_sensitive_data(data, key): | |
fernet = Fernet(key) | |
return fernet.encrypt(data.encode()) | |
def decrypt_sensitive_data(encrypted_data, key): | |
fernet = Fernet(key) | |
return fernet.decrypt(encrypted_data).decode() | |
``` | |
- **Purpose**: Create reusable functions for making HTTP requests. | |
- **Example**: | |
```python | |
import httpx | |
def make_get_request(url, headers): | |
try: | |
response = httpx.get(url, headers=headers) | |
response.raise_for_status() | |
return response.json() | |
except httpx.HTTPStatusError as e: | |
logging.error(f"HTTP error occurred: {e}") | |
except Exception as e: | |
logging.error(f"An error occurred: {e}") | |
``` | |
- **Purpose**: Implement functions for specific tasks, such as listing fine-tuning jobs. | |
- **Example**: | |
```python | |
def list_fine_tuning_jobs(): | |
url = f"{get_api_endpoint()}/fine-tuning/jobs" | |
headers = {'Authorization': f'Bearer {get_api_key()}'} | |
return make_get_request(url, headers) | |
``` | |
- **Purpose**: Validate user inputs to ensure they meet the required criteria. | |
- **Example**: | |
```python | |
def validate_file_path(file_path): | |
if not os.path.exists(file_path): | |
raise ValueError(f"File path {file_path} does not exist.") | |
``` | |
Include the following functions and concepts from the `ultimatethinking.txt` file: | |
- **Encrypt and Decrypt Sensitive Data**: Functions to encrypt and decrypt sensitive information using `cryptography.fernet`. | |
- **Logging Setup**: Function to set up logging based on configuration. | |
- **Load JSON Configuration**: Function to load configuration from a JSON file. | |
- **Universal Reasoning Aggregator**: Class to aggregate responses from multiple perspectives and handle context history, sentiment analysis, and real-time data fetching. | |
- **Element Class**: Class to define elements with properties and defense abilities. | |
- **Custom Recognizer**: Class to recognize keywords and intents from user input. | |
- **Voice and Image Input Handling**: Functions to handle voice and image inputs using `speech_recognition` and `PIL`. | |
```python | |
if __name__ == "__main__": | |
setup_logging() | |
jobs = list_fine_tuning_jobs() | |
if jobs: | |
logging.info(f"Fine-tuning jobs: {jobs}") | |
``` | |
--- | |