The Chat API provides methods for interacting with Goodfire’s language models in a chat format.
The base chat interface is OpenAI-compatible. It supports both streaming and non-streaming completions, as well as logits computation.Once you have a model variant, you can use it to create chat completions.
# Initialize the clientclient = goodfire.Client( '{YOUR_API_KEY}',)# Create a non-streaming completionresponse = client.chat.completions.create( messages=[ {"role": "user", "content": "What is the capital of France?"} ], model="meta-llama/Llama-3.1-8B-Instruct")print(response.choices[0].message["content"])
# Create a variant with feature modificationsvariant = goodfire.Variant("meta-llama/Llama-3.1-8B-Instruct")pirate_features = client.features.search( "talk like a pirate", model=variant, top_k=1)variant.set(pirate_features[0], 0.5)# Use the variant in chat completionfor token in client.chat.completions.create( messages=[ {"role": "user", "content": "Tell me about the ocean."} ], model=variant, stream=True, max_completion_tokens=100,): print(token.choices[0].delta.content, end="")
logits = client.chat.logits( messages=[ {"role": "user", "content": "The capital of France is"} ], model="meta-llama/Llama-3.1-8B-Instruct", filter_vocabulary=["Paris", "London", "Berlin"])print(logits.logits)
If stream=True: Iterator of StreamingChatCompletionChunk objects
Examples:Non-streaming completion:
Basic Chat Completion
Copy
response = client.chat.completions.create( messages=[ {"role": "user", "content": "What is the capital of France?"} ], model="meta-llama/Llama-3.1-8B-Instruct")print(response.choices[0].message["content"])
Streaming completion:
Streaming Chat Completion
Copy
for chunk in client.chat.completions.create( messages=[ {"role": "user", "content": "Write a short poem"} ], model="meta-llama/Llama-3.1-8B-Instruct", stream=True): print(chunk.choices[0].delta.content, end="")
logits = client.chat.logits( messages=[ {"role": "user", "content": "The capital of France is"} ], model="meta-llama/Llama-3.1-8B-Instruct", filter_vocabulary=["Paris", "London", "Berlin"])print(logits.logits)