updates
This commit is contained in:
73 files changed
+3290
-4522
No files matched your search
@@ -0,0 +1,21 @@
|
||||
from devgen.providers.anthropic import AnthropicProvider
|
||||
from devgen.providers.gemini import GeminiProvider
|
||||
from devgen.providers.huggingface import HuggingfaceProvider
|
||||
from devgen.providers.openai import OpenaiProvider
|
||||
from devgen.providers.openrouter import OpenrouterProvider
|
||||
|
||||
|
||||
def get_provider(name):
|
||||
name_lower = name.lower()
|
||||
if name_lower == "gemini":
|
||||
return GeminiProvider()
|
||||
elif name_lower == "openai":
|
||||
return OpenaiProvider()
|
||||
elif name_lower == "huggingface":
|
||||
return HuggingfaceProvider()
|
||||
elif name_lower == "openrouter":
|
||||
return OpenrouterProvider()
|
||||
elif name_lower == "anthropic":
|
||||
return AnthropicProvider()
|
||||
|
||||
raise NotImplementedError(f"Provider '{name}' is not implemented yet.")
|
||||
@@ -0,0 +1,23 @@
|
||||
import anthropic
|
||||
|
||||
|
||||
class AnthropicProvider:
|
||||
"""Generates content using Anthropic's Claude models."""
|
||||
|
||||
def generate(
|
||||
self, prompt: str, api_key: str, model: str = "claude-3-opus-20240229", **kwargs
|
||||
) -> str:
|
||||
"""Generates a response using the Anthropic API."""
|
||||
if not api_key:
|
||||
raise ValueError("Anthropic API key is required.")
|
||||
|
||||
try:
|
||||
client = anthropic.Anthropic(api_key=api_key)
|
||||
message = client.messages.create(
|
||||
model=model,
|
||||
max_tokens=1024,
|
||||
messages=[{"role": "user", "content": prompt}],
|
||||
)
|
||||
return message.content[0].text.strip()
|
||||
except Exception as e:
|
||||
raise RuntimeError(f"Anthropic generation failed: {e}")
|
||||
@@ -0,0 +1,24 @@
|
||||
import google.generativeai as genai
|
||||
|
||||
|
||||
class GeminiProvider:
|
||||
"""Generates content using Google's Gemini models."""
|
||||
|
||||
def generate(
|
||||
self, prompt: str, api_key: str, model: str = "gemini-pro", **kwargs
|
||||
) -> str:
|
||||
"""Generates a response using the Gemini API."""
|
||||
if not api_key:
|
||||
raise ValueError("Gemini API key is required.")
|
||||
|
||||
genai.configure(api_key=api_key)
|
||||
|
||||
# Handle model name mapping if needed, or trust user input
|
||||
# gemini-pro is a common default
|
||||
|
||||
try:
|
||||
model_instance = genai.GenerativeModel(model)
|
||||
response = model_instance.generate_content(prompt)
|
||||
return response.text.strip()
|
||||
except Exception as e:
|
||||
raise RuntimeError(f"Gemini generation failed: {e}")
|
||||
@@ -0,0 +1,45 @@
|
||||
import requests
|
||||
|
||||
|
||||
class HuggingfaceProvider:
|
||||
"""Generates content using Hugging Face Inference API."""
|
||||
|
||||
API_URL_TEMPLATE = "https://api-inference.huggingface.co/models/{model}"
|
||||
|
||||
def generate(
|
||||
self,
|
||||
prompt: str,
|
||||
api_key: str,
|
||||
model: str = "mistralai/Mistral-7B-Instruct-v0.2",
|
||||
**kwargs,
|
||||
) -> str:
|
||||
"""Generates a response using Hugging Face API."""
|
||||
if not api_key:
|
||||
raise ValueError("Hugging Face API token is required.")
|
||||
|
||||
api_url = self.API_URL_TEMPLATE.format(model=model)
|
||||
headers = {"Authorization": f"Bearer {api_key}"}
|
||||
|
||||
# HF models often expect specific prompting formats, but we'll send raw prompt
|
||||
# Some models are text-generation, some are conversational.
|
||||
# Assuming text-generation for generic usage.
|
||||
|
||||
payload = {
|
||||
"inputs": prompt,
|
||||
"parameters": {"max_new_tokens": 500, "return_full_text": False},
|
||||
}
|
||||
|
||||
try:
|
||||
response = requests.post(api_url, headers=headers, json=payload)
|
||||
response.raise_for_status()
|
||||
result = response.json()
|
||||
|
||||
if isinstance(result, list) and "generated_text" in result[0]:
|
||||
return result[0]["generated_text"].strip()
|
||||
elif isinstance(result, dict) and "error" in result:
|
||||
raise RuntimeError(f"Hugging Face API error: {result['error']}")
|
||||
else:
|
||||
return str(result)
|
||||
|
||||
except Exception as e:
|
||||
raise RuntimeError(f"Hugging Face generation failed: {e}")
|
||||
@@ -0,0 +1,48 @@
|
||||
from openai import OpenAI
|
||||
|
||||
|
||||
class OpenaiProvider:
|
||||
"""Generates a response string using the OpenAI ChatCompletion API based on the provided prompt and parameters. This method initializes an OpenAI client with the given API key, sends a chat completion request with specified model and additional parameters, and returns the content of the generated message.
|
||||
|
||||
Args:
|
||||
prompt (str): The input prompt to generate a response for.
|
||||
api_key (str): The API key used to authenticate with the OpenAI service.
|
||||
model (str, optional): The model to use for generation; defaults to "gpt-4o".
|
||||
**kwargs: Additional keyword arguments to customize the API request (e.g., temperature).
|
||||
|
||||
Returns:
|
||||
str: The content of the generated response message.
|
||||
"""
|
||||
|
||||
DEFAULT_MODEL = "gpt-4o"
|
||||
|
||||
def generate(
|
||||
self, prompt: str, api_key: str, model: str | None = None, **kwargs
|
||||
) -> str:
|
||||
"""Generates a response from the OpenAI ChatCompletion API based on the provided prompt and parameters.
|
||||
|
||||
Creates a client instance with the specified API key, sends a chat completion request using the selected model and additional parameters, and returns the generated message content as a string.
|
||||
|
||||
Args:
|
||||
prompt (str): Prompt input.
|
||||
api_key (str): OpenAI API key.
|
||||
model (str, optional): Model to use (default: gpt-4o).
|
||||
**kwargs: Additional OpenAI ChatCompletion parameters (e.g., temperature).
|
||||
|
||||
Returns:
|
||||
str: Generated response content.
|
||||
"""
|
||||
# 1. Create a client instance with the API key.
|
||||
try:
|
||||
client = OpenAI(api_key=api_key)
|
||||
except Exception as e:
|
||||
# Add error handling if the client fails to initialize
|
||||
raise RuntimeError(f"Failed to initialize OpenAI client: {e}")
|
||||
|
||||
# 2. Use the modern API syntax: client.chat.completions.create
|
||||
response = client.chat.completions.create(
|
||||
model=model or self.DEFAULT_MODEL,
|
||||
messages=[{"role": "user", "content": prompt}],
|
||||
**kwargs,
|
||||
)
|
||||
return response.choices[0].message.content.strip()
|
||||
@@ -0,0 +1,33 @@
|
||||
from openai import OpenAI
|
||||
|
||||
|
||||
class OpenrouterProvider:
|
||||
"""Generates content using OpenRouter (OpenAI-compatible API)."""
|
||||
|
||||
BASE_URL = "https://openrouter.ai/api/v1"
|
||||
|
||||
def generate(
|
||||
self, prompt: str, api_key: str, model: str = "openai/gpt-3.5-turbo", **kwargs
|
||||
) -> str:
|
||||
"""Generates a response using OpenRouter."""
|
||||
if not api_key:
|
||||
raise ValueError("OpenRouter API key is required.")
|
||||
|
||||
try:
|
||||
client = OpenAI(
|
||||
base_url=self.BASE_URL,
|
||||
api_key=api_key,
|
||||
)
|
||||
|
||||
response = client.chat.completions.create(
|
||||
model=model,
|
||||
messages=[{"role": "user", "content": prompt}],
|
||||
extra_headers={
|
||||
"HTTP-Referer": "https://github.com/S4NKALP/devgen", # Optional
|
||||
"X-Title": "devgen CLI", # Optional
|
||||
},
|
||||
**kwargs,
|
||||
)
|
||||
return response.choices[0].message.content.strip()
|
||||
except Exception as e:
|
||||
raise RuntimeError(f"OpenRouter generation failed: {e}")
|
||||
Reference in new issue
Block a user