mirror of
https://github.com/kennethreitz/simplemind.git
synced 2026-06-05 22:50:18 +00:00
whoot
This commit is contained in:
@@ -72,8 +72,9 @@ SimpleMind also allows for easy conversational flows:
|
||||
```
|
||||
|
||||
## Supported APIs
|
||||
- **OpenAI GPT**
|
||||
= **Anthropic**
|
||||
- **OpenAI's GPT**
|
||||
- **Anthropic's Claude**
|
||||
- **X.AI's Grok**
|
||||
|
||||
## Why SimpleMind?
|
||||
- **Intuitive**: Built with Pythonic simplicity and readability in mind.
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
from .openai import OpenAI
|
||||
from .anthropic import Anthropic
|
||||
from .xai import XAI
|
||||
|
||||
providers = [OpenAI, Anthropic]
|
||||
providers = [OpenAI, Anthropic, XAI]
|
||||
|
||||
@@ -0,0 +1,80 @@
|
||||
import openai as oa
|
||||
import instructor
|
||||
|
||||
# from ..models import Conversation, Message
|
||||
from ..settings import settings
|
||||
|
||||
PROVIDER_NAME = "xai"
|
||||
DEFAULT_MODEL = "grok-beta"
|
||||
BASE_URL = "https://api.x.ai/v1"
|
||||
DEFAULT_MAX_TOKENS = 1000
|
||||
|
||||
|
||||
class XAI:
|
||||
__name__ = PROVIDER_NAME
|
||||
DEFAULT_MODEL = DEFAULT_MODEL
|
||||
|
||||
def __init__(self, api_key: str = None):
|
||||
self.api_key = api_key or settings.XAI_API_KEY
|
||||
|
||||
@property
|
||||
def client(self):
|
||||
"""The raw OpenAI client."""
|
||||
|
||||
return oa.OpenAI(
|
||||
api_key=settings.XAI_API_KEY,
|
||||
base_url="https://api.x.ai/v1",
|
||||
)
|
||||
|
||||
@property
|
||||
def structured_client(self):
|
||||
"""A client patched with Instructor."""
|
||||
return instructor.patch(
|
||||
oa.OpenAI(api_key=self.api_key, base_url="https://api.x.ai/v1")
|
||||
)
|
||||
|
||||
def send_conversation(self, conversation: "Conversation"):
|
||||
"""Send a conversation to the OpenAI API."""
|
||||
from ..models import Message
|
||||
|
||||
messages = [
|
||||
{"role": msg.role, "content": msg.text} for msg in conversation.messages
|
||||
]
|
||||
|
||||
response = self.client.chat.completions.create(
|
||||
model=conversation.llm_model or DEFAULT_MODEL, messages=messages
|
||||
)
|
||||
|
||||
# Get the response content from the OpenAI response
|
||||
assistant_message = response.choices[0].message
|
||||
|
||||
# Create and return a properly formatted Message instance
|
||||
return Message(
|
||||
role="assistant",
|
||||
text=assistant_message.content,
|
||||
raw=response,
|
||||
llm_model=conversation.llm_model or DEFAULT_MODEL,
|
||||
llm_provider=PROVIDER_NAME,
|
||||
)
|
||||
|
||||
def structured_response(self, prompt, response_model, *, llm_model):
|
||||
# Ensure messages are provided in kwargs
|
||||
messages = [
|
||||
{"role": "user", "content": prompt},
|
||||
]
|
||||
|
||||
response = self.structured_client.chat.completions.create(
|
||||
messages=messages, model=llm_model, response_model=response_model
|
||||
)
|
||||
return response
|
||||
|
||||
def generate_text(self, prompt, *, llm_model):
|
||||
messages = [
|
||||
{"role": "user", "content": prompt},
|
||||
]
|
||||
|
||||
response = self.client.chat.completions.create(
|
||||
messages=messages, model=llm_model
|
||||
)
|
||||
|
||||
return response.choices[0].message.content
|
||||
@@ -5,6 +5,7 @@ from pydantic_settings import BaseSettings
|
||||
class Settings(BaseSettings):
|
||||
OPENAI_API_KEY: str = Field(..., env="OPENAI_API_KEY")
|
||||
ANTHROPIC_API_KEY: str = Field(..., env="ANTHROPIC_API_KEY")
|
||||
XAI_API_KEY: str = Field(..., env="XAI_API_KEY")
|
||||
|
||||
|
||||
settings = Settings()
|
||||
|
||||
@@ -2,16 +2,12 @@ import simplemind as sm
|
||||
from pydantic import BaseModel
|
||||
|
||||
|
||||
class Poem(BaseModel):
|
||||
title: str
|
||||
content: str
|
||||
conversation = sm.create_conversation(llm_model="grok-beta", llm_provider="xai")
|
||||
|
||||
|
||||
output = sm.generate_data(
|
||||
"Write a poem about love",
|
||||
llm_model="gpt-4o-mini",
|
||||
llm_provider="openai",
|
||||
response_model=Poem,
|
||||
conversation.add_message(
|
||||
role="user",
|
||||
text="Write a poem about love",
|
||||
)
|
||||
r = conversation.send()
|
||||
|
||||
print(output)
|
||||
print(r)
|
||||
|
||||
Reference in New Issue
Block a user