SDKs
This guide will help you get started with the Writer SDKs. Follow these steps to install the SDKs and perform basic operations.
Your API key can be generated using these steps.
Prerequisites
- Python 3.7 or higher and pip installed on your system
- A Writer API key
Installation
Open your terminal or command prompt and install the Writer SDK using npm:
pip install writer-sdk
Setting up the Writer client
Create a new file
Create a new Python file (e.g., writer-test.py
).
Import and initialize
Import the Writer SDK and create a client instance, replacing your-api-key
with your actual API key:
from writerai import Writer
client = Writer(
api_key="your-api-key" # Replace with your API key
)
For production usage, we recommend using a library like python-dotenv to manage your API key in a .env
file as WRITER_API_KEY
.
Basic usage: Chat completion
To get started using Writer LLMs and the API, perform a basic chat completion task.
Add chat completion code
Add the following code to your file:
def main():
try:
response = client.chat.chat(
messages=[{ "content": "Write a poem about Python", "role": 'user' }],
model="palmyra-x-004",
stream=True,
)
output_text = ""
for chunk in response:
if chunk.choices[0].delta.content:
output_text += chunk.choices[0].delta.content:
else:
continue
print(output_text)
except Exception as error:
print("Error:", error)
if __name__ == "__main__":
main()
Run the script
Save the file and run it using Python:
python writer-test.py
You should see the chat response streaming to the console.
For advanced SDK usage, such as custom requests or configuring retries and timeouts, refer to the README file in the GitHub repository.
Next steps
Now that you’re set up with the SDKs, check out the guides on text generation, applications, and Knowledge Graph to explore more advanced features of the Writer platform. You can also use the API reference to learn more detailed information about available endpoints.