Allow AI interactions to be proxied through Zed's server so you don't need an API key (#7367)
Co-authored-by: Antonio <antonio@zed.dev> Resurrected this from some assistant work I did in Spring of 2023. - [x] Resurrect streaming responses - [x] Use streaming responses to enable AI via Zed's servers by default (but preserve API key option for now) - [x] Simplify protobuf - [x] Proxy to OpenAI on zed.dev - [x] Proxy to Gemini on zed.dev - [x] Improve UX for switching between openAI and google models - We current disallow cycling when setting a custom model, but we need a better solution to keep OpenAI models available while testing the google ones - [x] Show remaining tokens correctly for Google models - [x] Remove semantic index - [x] Delete `ai` crate - [x] Cloud front so we can ban abuse - [x] Rate-limiting - [x] Fix panic when using inline assistant - [x] Double check the upgraded `AssistantSettings` are backwards-compatible - [x] Add hosted LLM interaction behind a `language-models` feature flag. Release Notes: - We are temporarily removing the semantic index in order to redesign it from scratch. --------- Co-authored-by: Antonio <antonio@zed.dev> Co-authored-by: Antonio Scandurra <me@as-cii.com> Co-authored-by: Thorsten <thorsten@zed.dev> Co-authored-by: Max <max@zed.dev>
This commit is contained in:
parent
905a24079a
commit
8ae5a3b61a
87 changed files with 3647 additions and 8937 deletions
|
@ -11,3 +11,8 @@ cargo run -p collab -- migrate
|
|||
|
||||
echo "seeding database..."
|
||||
script/seed-db
|
||||
|
||||
if [[ "$OSTYPE" == "linux-gnu"* ]]; then
|
||||
echo "Linux dependencies..."
|
||||
script/linux
|
||||
fi
|
||||
|
|
|
@ -1,3 +0,0 @@
|
|||
#!/bin/bash
|
||||
|
||||
RUST_LOG=semantic_index=trace cargo run --example semantic_index_eval --release
|
91
script/gemini.py
Normal file
91
script/gemini.py
Normal file
|
@ -0,0 +1,91 @@
|
|||
import subprocess
|
||||
import json
|
||||
import http.client
|
||||
import mimetypes
|
||||
import os
|
||||
|
||||
def get_text_files():
|
||||
text_files = []
|
||||
# List all files tracked by Git
|
||||
git_files_proc = subprocess.run(['git', 'ls-files'], stdout=subprocess.PIPE, text=True)
|
||||
for file in git_files_proc.stdout.strip().split('\n'):
|
||||
# Check MIME type for each file
|
||||
mime_check_proc = subprocess.run(['file', '--mime', file], stdout=subprocess.PIPE, text=True)
|
||||
if 'text' in mime_check_proc.stdout:
|
||||
text_files.append(file)
|
||||
|
||||
print(f"File count: {len(text_files)}")
|
||||
|
||||
return text_files
|
||||
|
||||
def get_file_contents(file):
|
||||
# Read file content
|
||||
with open(file, 'r') as f:
|
||||
return f.read()
|
||||
|
||||
|
||||
def main():
|
||||
GEMINI_API_KEY = os.environ.get('GEMINI_API_KEY')
|
||||
|
||||
# Your prompt
|
||||
prompt = "Document the data types and dataflow in this codebase in preparation to port a streaming implementation to rust:\n\n"
|
||||
# Fetch all text files
|
||||
text_files = get_text_files()
|
||||
code_blocks = []
|
||||
for file in text_files:
|
||||
file_contents = get_file_contents(file)
|
||||
# Create a code block for each text file
|
||||
code_blocks.append(f"\n`{file}`\n\n```{file_contents}```\n")
|
||||
|
||||
# Construct the JSON payload
|
||||
payload = json.dumps({
|
||||
"contents": [{
|
||||
"parts": [{
|
||||
"text": prompt + "".join(code_blocks)
|
||||
}]
|
||||
}]
|
||||
})
|
||||
|
||||
# Prepare the HTTP connection
|
||||
conn = http.client.HTTPSConnection("generativelanguage.googleapis.com")
|
||||
|
||||
# Define headers
|
||||
headers = {
|
||||
'Content-Type': 'application/json',
|
||||
'Content-Length': str(len(payload))
|
||||
}
|
||||
|
||||
# Output the content length in bytes
|
||||
print(f"Content Length in kilobytes: {len(payload.encode('utf-8')) / 1024:.2f} KB")
|
||||
|
||||
|
||||
# Send a request to count the tokens
|
||||
conn.request("POST", f"/v1beta/models/gemini-1.5-pro-latest:countTokens?key={GEMINI_API_KEY}", body=payload, headers=headers)
|
||||
# Get the response
|
||||
response = conn.getresponse()
|
||||
if response.status == 200:
|
||||
token_count = json.loads(response.read().decode('utf-8')).get('totalTokens')
|
||||
print(f"Token count: {token_count}")
|
||||
else:
|
||||
print(f"Failed to get token count. Status code: {response.status}, Response body: {response.read().decode('utf-8')}")
|
||||
|
||||
|
||||
# Prepare the HTTP connection
|
||||
conn = http.client.HTTPSConnection("generativelanguage.googleapis.com")
|
||||
conn.request("GET", f"/v1beta/models/gemini-1.5-pro-latest:streamGenerateContent?key={GEMINI_API_KEY}", body=payload, headers=headers)
|
||||
|
||||
# Get the response in a streaming manner
|
||||
response = conn.getresponse()
|
||||
if response.status == 200:
|
||||
print("Successfully sent the data to the API.")
|
||||
# Read the response in chunks
|
||||
while chunk := response.read(4096):
|
||||
print(chunk.decode('utf-8'))
|
||||
else:
|
||||
print(f"Failed to send the data to the API. Status code: {response.status}, Response body: {response.read().decode('utf-8')}")
|
||||
|
||||
# Close the connection
|
||||
conn.close()
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
|
@ -1,4 +1,6 @@
|
|||
#!/usr/bin/bash -e
|
||||
#!/usr/bin/bash
|
||||
|
||||
set -e
|
||||
|
||||
# if sudo is not installed, define an empty alias
|
||||
maysudo=$(command -v sudo || command -v doas || true)
|
||||
|
|
1
script/script.py
Normal file
1
script/script.py
Normal file
|
@ -0,0 +1 @@
|
|||
|
|
@ -3,12 +3,15 @@
|
|||
set -e
|
||||
|
||||
# Install sqlx-cli if needed
|
||||
[[ "$(sqlx --version)" == "sqlx-cli 0.5.7" ]] || cargo install sqlx-cli --version 0.5.7
|
||||
if [[ "$(sqlx --version)" != "sqlx-cli 0.5.7" ]]; then
|
||||
echo "sqlx-cli not found or not the required version, installing version 0.5.7..."
|
||||
cargo install sqlx-cli --version 0.5.7
|
||||
fi
|
||||
|
||||
cd crates/collab
|
||||
|
||||
# Export contents of .env.toml
|
||||
eval "$(cargo run --quiet --bin dotenv)"
|
||||
eval "$(cargo run --bin dotenv)"
|
||||
|
||||
# Run sqlx command
|
||||
sqlx $@
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue