This guide provides a comprehensive walkthrough of the HelpingAI Python SDK, from installation to advanced usage.
Install the HelpingAI SDK using pip:
pip install HelpingAIYour API key is available on your HelpingAI Dashboard.
We recommend using an environment variable to store your API key. This keeps your key secure and out of your codebase.
export HAI_API_KEY='your-api-key-here'Create an instance of the HAI client. If the HAI_API_KEY environment variable is set, the client will automatically use it. Otherwise, you can pass the key directly.
from HelpingAI import HAI
# Initialize with environment variable {#initialize-with-environment-variable}
client = HAI()
# Or, initialize with key directly {#or-initialize-with-key-directly}
# client = HAI(api_key="your-api-key-here") {#client-haiapi_keyyour-api-key-here}You can now use the client to make API calls. For example, you can create a chat completion:
response = client.chat.completions.create(
model="Dhanishtha-2.0-preview",
messages=[
{"role": "user", "content": "Hello, world!"}
]
)
print(response.choices[0].message.content)To stream responses, set the stream parameter to True.
stream = client.chat.completions.create(
model="Dhanishtha-2.0-preview",
messages=[
{"role": "user", "content": "Tell me a story about a brave knight."}
],
stream=True
)
for chunk in stream:
if chunk.choices[0].delta.content:
print(chunk.choices[0].delta.content, end="")The HelpingAI SDK supports tool calling, allowing you to extend the capabilities of the models.
Use the @tools decorator to define a tool.
from HelpingAI.tools import tools
@tools
def get_weather(city: str, unit: str = "celsius") -> dict:
"""Get the current weather in a given city.
Args:
city (str): The city for which to get the weather.
unit (str): The unit to use for the temperature. Can be either "celsius" or "fahrenheit".
"""
return {"city": city, "temperature": 22, "unit": unit}Pass the tool to the tools parameter of the client.chat.completions.create() method.
from HelpingAI.tools import get_tools
response = client.chat.completions.create(
model="Dhanishtha-2.0-preview",
messages=[{"role": "user", "content": "What's the weather in Paris?"}],
tools=get_tools(),
tool_choice="auto"
)If the model decides to use the tool, you can execute it using client.call().
import json
message = response.choices[0].message
if message.tool_calls:
tool_messages = []
for tool_call in message.tool_calls:
function_name = tool_call.function.name
function_args = json.loads(tool_call.function.arguments)
result = client.call(function_name, function_args)
tool_messages.append({
"role": "tool",
"tool_call_id": tool_call.id,
"name": function_name,
"content": json.dumps(result)
})
follow_up_response = client.chat.completions.create(
model="Dhanishtha-2.0-preview",
messages=[
{"role": "user", "content": "What's the weather in Paris?"},
message,
*tool_messages,
]
)
print(follow_up_response.choices[0].message.content)Now that you have the basics down, you can explore the full capabilities of the SDK: