Skip to content

Commit d720224

Browse files
use openai functions (#112)
1 parent 5fad68f commit d720224

File tree

4 files changed

+90
-573
lines changed

4 files changed

+90
-573
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "solana-agent"
3-
version = "29.2.0"
3+
version = "29.2.1"
44
description = "AI Agents for Solana"
55
authors = ["Bevan Hunt <bevan@bevanhunt.com>"]
66
license = "MIT"

solana_agent/adapters/openai_adapter.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,10 @@ async def generate_text(
163163
api_key: Optional[str] = None,
164164
base_url: Optional[str] = None,
165165
model: Optional[str] = None,
166-
) -> str: # pragma: no cover
167-
"""Generate text from OpenAI models as a single string (no images)."""
166+
functions: Optional[List[Dict[str, Any]]] = None,
167+
function_call: Optional[Union[str, Dict[str, Any]]] = None,
168+
) -> Any: # pragma: no cover
169+
"""Generate text or function call from OpenAI models."""
168170
messages = []
169171
if system_prompt:
170172
messages.append({"role": "system", "content": system_prompt})
@@ -174,6 +176,10 @@ async def generate_text(
174176
"messages": messages,
175177
"model": model or self.text_model,
176178
}
179+
if functions:
180+
request_params["functions"] = functions
181+
if function_call:
182+
request_params["function_call"] = function_call
177183

178184
if api_key and base_url:
179185
client = AsyncOpenAI(api_key=api_key, base_url=base_url)
@@ -185,17 +191,13 @@ async def generate_text(
185191

186192
try:
187193
response = await client.chat.completions.create(**request_params)
188-
if response.choices and response.choices[0].message.content:
189-
return response.choices[0].message.content
190-
else:
191-
logger.warning("Received non-streaming response with no content.")
192-
return ""
193-
except OpenAIError as e: # Catch specific OpenAI errors
194+
return response
195+
except OpenAIError as e:
194196
logger.error(f"OpenAI API error during text generation: {e}")
195-
return f"I apologize, but I encountered an API error: {e}"
197+
return None
196198
except Exception as e:
197199
logger.exception(f"Error in generate_text: {e}")
198-
return f"I apologize, but I encountered an unexpected error: {e}"
200+
return None
199201

200202
def _calculate_gpt41_image_cost(self, width: int, height: int, model: str) -> int:
201203
"""Calculates the token cost for an image with GPT-4.1 models."""

solana_agent/interfaces/providers/llm.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
from abc import ABC, abstractmethod
22
from typing import (
3+
Any,
34
AsyncGenerator,
5+
Dict,
46
List,
57
Literal,
68
Optional,
@@ -26,7 +28,9 @@ async def generate_text(
2628
api_key: Optional[str] = None,
2729
base_url: Optional[str] = None,
2830
model: Optional[str] = None,
29-
) -> str:
31+
functions: Optional[List[Dict[str, Any]]] = None,
32+
function_call: Optional[Union[str, Dict[str, Any]]] = None,
33+
) -> Any:
3034
"""Generate text from the language model."""
3135
pass
3236

0 commit comments

Comments
 (0)