Language Model API¶
Access VS Code's Language Model API to interact with GitHub Copilot and other AI models directly from Python.
Overview¶
The Language Model API enables Python scripts to: - Query available language models (Copilot, custom providers) - Send chat requests with conversation history - Generate code, documentation, and explanations - Count tokens for cost estimation - Build AI-powered automation workflows
LanguageModel
¶
Language Model API for interacting with Copilot and other LLMs.
This class provides access to VS Code's Language Model API, allowing Python scripts to select and interact with available language models.
Initialize the language model API.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
client
|
VSCodeClient
|
VSCode client instance |
required |
Source code in vscode_sockpuppet/language_model.py
select_chat_models
¶
select_chat_models(vendor: Optional[str] = None, family: Optional[str] = None, version: Optional[str] = None, id: Optional[str] = None) -> List[LanguageModelChat]
Select language models matching the given criteria.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vendor
|
Optional[str]
|
Filter by vendor (e.g., 'copilot') |
None
|
family
|
Optional[str]
|
Filter by model family (e.g., 'gpt-4o') |
None
|
version
|
Optional[str]
|
Filter by version |
None
|
id
|
Optional[str]
|
Filter by specific model ID |
None
|
Returns:
| Type | Description |
|---|---|
List[LanguageModelChat]
|
List of matching language models |
Raises:
| Type | Description |
|---|---|
Exception
|
If no models match the criteria or models not available |
Example
Get all Copilot models¶
models = client.lm.select_chat_models(vendor='copilot')
Get specific model family¶
models = client.lm.select_chat_models( vendor='copilot', family='gpt-4o' )
Get specific model by ID¶
models = client.lm.select_chat_models( id='copilot-gpt-4o' )
Source code in vscode_sockpuppet/language_model.py
LanguageModelChat
¶
Represents a language model for making chat requests.
Initialize a language model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
client
|
VSCodeClient
|
VSCode client instance |
required |
model_data
|
Dict[str, Any]
|
Model metadata from VS Code |
required |
Source code in vscode_sockpuppet/language_model.py
max_input_tokens
property
¶
Maximum number of input tokens the model supports.
send_request
¶
send_request(messages: List[LanguageModelChatMessage], options: Optional[Dict[str, Any]] = None) -> Dict[str, Any]
Send a chat request to the language model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
messages
|
List[LanguageModelChatMessage]
|
List of chat messages |
required |
options
|
Optional[Dict[str, Any]]
|
Optional request options (e.g., temperature, top_p) |
None
|
Returns:
| Type | Description |
|---|---|
Dict[str, Any]
|
Response dictionary with 'text' (full response) and |
Dict[str, Any]
|
'parts' (individual fragments) |
Raises:
| Type | Description |
|---|---|
Exception
|
If the model is not available or request fails |
Example
messages = [ LanguageModelChatMessage.user("Explain async/await") ] response = model.send_request(messages) print(response['text'])
Source code in vscode_sockpuppet/language_model.py
count_tokens
¶
Count the number of tokens in a text string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The text to count tokens for |
required |
Returns:
| Type | Description |
|---|---|
int
|
Number of tokens |
Example
tokens = model.count_tokens("Hello, world!") print(f"Token count: {tokens}")
Source code in vscode_sockpuppet/language_model.py
__repr__
¶
LanguageModelChatMessage
¶
Represents a chat message for language model requests.
Create a chat message.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
role
|
str
|
Message role ('user' or 'assistant') |
required |
content
|
str
|
Message content |
required |
Source code in vscode_sockpuppet/language_model.py
user
classmethod
¶
Create a user message.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
content
|
str
|
The user's message content |
required |
Returns:
| Type | Description |
|---|---|
LanguageModelChatMessage
|
A chat message with role 'user' |
Example
msg = LanguageModelChatMessage.user("Explain this code")
Source code in vscode_sockpuppet/language_model.py
assistant
classmethod
¶
Create an assistant message.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
content
|
str
|
The assistant's message content |
required |
Returns:
| Type | Description |
|---|---|
LanguageModelChatMessage
|
A chat message with role 'assistant' |
Example
msg = LanguageModelChatMessage.assistant("This code...")
Source code in vscode_sockpuppet/language_model.py
Usage Examples¶
Basic Chat Request¶
from vscode_sockpuppet import VSCodeClient, LanguageModelChatMessage
client = VSCodeClient()
client.connect()
# Get available models
models = client.lm.select_chat_models(vendor='copilot')
model = models[0]
# Send a request
messages = [
LanguageModelChatMessage.user("Explain async/await in Python")
]
response = model.send_request(messages)
print(response['text'])
Multi-turn Conversation¶
# Start conversation
messages = [
LanguageModelChatMessage.user("What is a Python decorator?")
]
response = model.send_request(messages)
print(f"Assistant: {response['text']}")
# Add response to history
messages.append(
LanguageModelChatMessage.assistant(response['text'])
)
# Ask follow-up
messages.append(
LanguageModelChatMessage.user("Show me an example")
)
response = model.send_request(messages)
print(f"Assistant: {response['text']}")
Code Generation¶
messages = [
LanguageModelChatMessage.user(
"Write a Python function to check if a number is prime. "
"Include docstring and type hints."
)
]
response = model.send_request(messages)
generated_code = response['text']
print(generated_code)
Token Counting¶
# Count tokens before sending
text = "This is a long piece of text..."
token_count = model.count_tokens(text)
print(f"This will use approximately {token_count} tokens")
# Helpful for estimating costs and staying within limits
if token_count < model.max_input_tokens:
response = model.send_request([
LanguageModelChatMessage.user(text)
])
Code Explanation Workflow¶
# Get selected code from active editor
editor = client.editor.get_active_text_editor()
if editor and editor.selection:
doc = client.workspace.open_text_document(editor.document['uri'])
selected_text = doc.get_text_range(editor.selection)
# Ask model to explain
messages = [
LanguageModelChatMessage.user(
f"Explain this code:\n\n{selected_text}"
)
]
response = model.send_request(messages)
# Show explanation in VS Code
client.window.show_information_message(
f"Explanation: {response['text'][:200]}..."
)
Error Analysis¶
error_code = """
def divide(a, b):
return a / b
"""
error_message = "ZeroDivisionError: division by zero"
messages = [
LanguageModelChatMessage.user(
f"This code has an error:\n\n{error_code}\n\n"
f"Error: {error_message}\n\n"
"Explain the problem and suggest a fix."
)
]
response = model.send_request(messages)
print(response['text'])
Model Selection¶
# Get all available models
all_models = client.lm.select_chat_models()
for model in all_models:
print(f"{model.name}: {model.max_input_tokens:,} tokens")
# Get specific vendor
copilot_models = client.lm.select_chat_models(vendor='copilot')
# Get specific model family
gpt4_models = client.lm.select_chat_models(
vendor='copilot',
family='gpt-4o'
)
# Get by exact ID
specific = client.lm.select_chat_models(
id='copilot-gpt-4o'
)
Requirements¶
GitHub Copilot¶
To use the Language Model API with Copilot:
- Install the GitHub Copilot extension in VS Code
- Sign in to your GitHub account
- Have an active Copilot subscription
Custom Language Model Providers¶
VS Code's Language Model API supports custom providers. Any extension that
implements the LanguageModelChatProvider interface will be accessible
through this API.
Error Handling¶
try:
models = client.lm.select_chat_models(vendor='copilot')
if not models:
print("No models available - is Copilot installed?")
exit(1)
model = models[0]
response = model.send_request(messages)
except Exception as e:
if "LanguageModelError" in str(e):
print(f"Language model error: {e}")
# Could be: model not found, user consent not given,
# quota exceeded, content filtered, etc.
else:
print(f"Unexpected error: {e}")
Best Practices¶
1. Check Model Availability¶
models = client.lm.select_chat_models(vendor='copilot')
if not models:
client.window.show_warning_message(
"Copilot not available. Please sign in and try again."
)
exit(1)
2. Handle Conversation Context¶
# Keep conversation history for context
conversation = []
def ask(question: str) -> str:
conversation.append(LanguageModelChatMessage.user(question))
response = model.send_request(conversation)
answer = response['text']
conversation.append(LanguageModelChatMessage.assistant(answer))
return answer
3. Stay Within Token Limits¶
# Check if prompt fits within limits
prompt_tokens = model.count_tokens(user_prompt)
if prompt_tokens > model.max_input_tokens:
print(f"Prompt too long! {prompt_tokens}/{model.max_input_tokens}")
# Truncate or split the prompt
4. Provide Clear Instructions¶
# Be specific in your requests
messages = [
LanguageModelChatMessage.user(
"Write a Python function that:\n"
"1. Takes a list of numbers\n"
"2. Returns only the even numbers\n"
"3. Includes type hints\n"
"4. Has a docstring with an example\n"
"5. Uses list comprehension"
)
]
Use Cases¶
Documentation Generation¶
Generate docstrings, comments, and README content:
def generate_docstring(function_code: str) -> str:
messages = [
LanguageModelChatMessage.user(
f"Generate a comprehensive Google-style docstring "
f"for this function:\n\n{function_code}"
)
]
response = model.send_request(messages)
return response['text']
Code Review Assistant¶
Analyze code for issues and suggestions:
def review_code(code: str) -> str:
messages = [
LanguageModelChatMessage.user(
f"Review this code for:\n"
f"- Potential bugs\n"
f"- Performance issues\n"
f"- Best practices\n\n"
f"{code}"
)
]
response = model.send_request(messages)
return response['text']
Test Generation¶
Create unit tests automatically:
def generate_tests(function_code: str) -> str:
messages = [
LanguageModelChatMessage.user(
f"Generate pytest unit tests for this function:\n\n"
f"{function_code}\n\n"
f"Include edge cases and error conditions."
)
]
response = model.send_request(messages)
return response['text']
Refactoring Suggestions¶
Get improvement recommendations:
def suggest_refactoring(code: str) -> str:
messages = [
LanguageModelChatMessage.user(
f"Suggest refactoring improvements for:\n\n{code}\n\n"
f"Focus on readability and maintainability."
)
]
response = model.send_request(messages)
return response['text']
API Reference¶
See the full API documentation for detailed information about:
LanguageModelclass and methodsLanguageModelChatproperties and operationsLanguageModelChatMessagemessage construction- Error handling and exceptions
- Advanced options and parameters